我有一个类似于这样的 numpy 数组: arr = [.2 , .1, .2, .3, .2 ] 现在我想做的是从这个数组中抽样,使得所得到某一索引的概率取决于该索引处的概率。例如,我获得索引3的概率是 .3 有人知道任何巧妙的方法来实现吗?
你可以使用np.random.choice进行离散概率分布的加权采样,只需将采样分布作为参数p提供即可。import numpy as np x = [.2 , .1, .2, .3, .2 ] # sample from `x` 100 times according to `x` n_samples = 100 samples = np.random.choice(x, n_samples, p=x)
numpy.random.choice
中的p
参数。 - user8408080