python:numpy从数组中随机抽取元素
# 以0.5的概率选取0或1生成10个数据np.random.choice([0,1],size=10,p=[.5,.5])# [0 1 0 0 1 0 0 0 0 1]# 生成英语“阅读答案”np.random.choice(['A', 'B', 'C', 'D'], 20, p=[0.25, 0.25, 0.25, 0.25])# ['D' 'D' 'A' 'B' 'A' 'C' 'C' 'D
·
# 以0.5的概率选取0或1生成10个数据
np.random.choice([0,1],size=10,p=[.5,.5])
# [0 1 0 0 1 0 0 0 0 1]
# 生成英语“阅读答案”
np.random.choice(['A', 'B', 'C', 'D'], 20, p=[0.25, 0.25, 0.25, 0.25])
# ['D' 'D' 'A' 'B' 'A' 'C' 'C' 'D' 'D' 'B' 'A' 'D' 'A' 'C' 'D' 'D' 'D' 'B'
'D' 'C']
numpy.random.choice(a, size=None, replace=True, p=None)
可以从一个int数字或1维array里随机选取内容,并将选取结果放入n维array中返回。
Parameters:
a : 1-D array-like or int
If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a were np.arange(a)
size : int or tuple of ints,可选参数
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
replace : boolean,可选参数
Whether the sample is with or without replacement
p : 1-D array-like, 可选参数
The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
更多推荐
已为社区贡献6条内容
所有评论(0)