Qua*_*lis 83
从numpy版本1.7开始,您可以使用numpy.random.choice():
elements = ['one', 'two', 'three']
weights = [0.2, 0.3, 0.5]
from numpy.random import choice
print(choice(elements, p=weights))
Run Code Online (Sandbox Code Playgroud)
Est*_*eis 34
从Python 3.6开始,您可以使用加权随机选择(替换)random.choices.
随机.选择(人口,权重=无,*,cum_weights =无,k = 1)
用法示例:
import random
random.choices(['one', 'two', 'three'], [0.2, 0.3, 0.5], k=10)
# ['three', 'two', 'three', 'three', 'three',
# 'three', 'three', 'two', 'two', 'one']
Run Code Online (Sandbox Code Playgroud)
Mis*_*cht 11
此函数有两个参数:权重列表和包含可供选择对象的列表:
from numpy import cumsum
from numpy.random import rand
def weightedChoice(weights, objects):
"""Return a random item from objects, with the weighting defined by weights
(which must sum to 1)."""
cs = cumsum(weights) #An array of the weights, cumulatively summed.
idx = sum(cs < rand()) #Find the index of the first weight over a random value.
return objects[idx]
Run Code Online (Sandbox Code Playgroud)
它不使用任何python循环.
您可以使用多项分布(来自 numpy)来做您想做的事。例如
elements = ['one', 'two', 'three']
weights = [0.2, 0.3, 0.5]
import numpy as np
indices = np.random.multinomial( 100, weights, 1)
#=> array([[20, 32, 48]]), YMMV
results = [] #A list of the original items, repeated the correct number of times.
for i, count in enumerate(indices[0]):
results.extend( [elements[i]]*count )
Run Code Online (Sandbox Code Playgroud)
所以第一个位置的元素出现了 20 次,第二个位置的元素出现了 32 次,第三个位置的元素出现了 48 次,这大致是给定权重的预期。
如果您很难理解多项式分布,我发现文档真的很有帮助。