我想知道Python中加权随机的方法。
1:10%,2:10%,3:10%,4:50%,5:20%
然后我选择一个没有重复的随机数。我应该如何编码?通常,我们会在下面编码:
Python
from random import *
sample(range(1,6),1)
您应该看看random.choices(https://docs.python.org/3/library/random.html#random.choices),它允许您定义权重,如果您使用的是python 3.6或更新的
示例:
import random
choices = [1,2,3,4,5]
random.choices(choices, weights=[10,10,10,50,20], k=20)
输出:
[3, 5, 2, 4, 4, 4, 5, 3, 5, 4, 5, 4, 5, 4, 2, 4, 5, 2, 4, 4]
试试这个:
from numpy.random import choice
list_of_candidates = [1,2,5,4,12]
number_of_items_to_pick = 120
p = [0.1, 0, 0.3, 0.6, 0]
choice(list_of_candidates, number_of_items_to_pick, p=probability_distribution)
如果你真的想要一个样本版本,你可以相应地准备范围:
nums = [1,2,3,4,5]
w = [10,10,10,50,20] # total of 100%
d = [x for y in ( [n]*i for n,i in zip(nums,w)) for x in y]
a_sample = random.sample(d,k=5)
print(a_sample)
print(d)
输出:
# 5 samples
[4, 2, 3, 1, 4]
# the whole sample input:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
如果你只需要一个数字,你可以使用随机选项-它被限制为1个数字,因为它的图纸是替换的。
import random
from collections import Counter
# draw and count 10k to show distribution works
print(Counter( random.choices([1,2,3,4,5], weights=[10,10,10,50,20], k=10000)).most_common())
输出:
[(4, 5019), (5, 2073), (3, 1031), (1, 978), (2, 899)]
使用"样本"(不替换(和"加权"(对我来说(是错误的,因为你会改变每个连续数字的权重,因为你从范围中删除了可用的数字(这是凭感觉-我猜告诉我背后的数学不是这样的(。