在python中,我应该如何加权随机编码?

在python中,我应该如何加权随机编码?,python,python-3.x,Python,Python 3.x,我想知道Python中加权随机数的方法 1:10%、2:10%、3:10%、4:50%、5:20% 然后我选择没有重复的随机数。我应该如何编码?一般来说,我们将在下面编写代码: Python from random import * sample(range(1,6),1) 您应该看看random.choices(),如果您使用的是python 3.6或更高版本,它允许您定义权重 例如: import random choices = [1,2,3,4,5] random.choices(ch

我想知道Python中加权随机数的方法

1:10%、2:10%、3:10%、4:50%、5:20%

然后我选择没有重复的随机数。我应该如何编码?一般来说,我们将在下面编写代码:

Python

from random import *
sample(range(1,6),1)

您应该看看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]
# 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]
[(4, 5019), (5, 2073), (3, 1031), (1, 978), (2, 899)]

如果您确实需要样本版本,您可以相应地准备范围:

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)
输出:

[3, 5, 2, 4, 4, 4, 5, 3, 5, 4, 5, 4, 5, 4, 2, 4, 5, 2, 4, 4]
# 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]
[(4, 5019), (5, 2073), (3, 1031), (1, 978), (2, 899)]
如果您只需要一个数字,您可以使用-它被限制为一个数字,因为它的图纸与替换

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())
输出:

[3, 5, 2, 4, 4, 4, 5, 3, 5, 4, 5, 4, 5, 4, 2, 4, 5, 2, 4, 4]
# 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]
[(4, 5019), (5, 2073), (3, 1031), (1, 978), (2, 899)]

使用“样本”不替换和“加权”是(对我来说)不正确的-因为你会改变每个连续数字的权重,因为你从范围中删除了可用的数字(这是凭感觉-我猜是后面的数学告诉我不是这样的)。

试试这个:

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)

重复答案的可能重复再加上
加权样本
并不是很好,所以在这里添加一个特定的答案。它没有重复吗?