简体   繁体   English

random.choices中的累积权重

[英]cumulative weights in random.choices

I am confused using the new random.choices in Python 3.6. 我很困惑使用Python 3.6中的新random.choices

Here is the doc : 这是文档

 random.choices(population, weights=None, *, cum_weights=None, k=1) 

-- Return a k sized list of elements chosen from the population with replacement. - 返回从替换人群中选择的k大小的元素列表。 If the population is empty, raises IndexError . 如果填充为空,则引发IndexError

They give an example: weights=[10, 5, 30, 5] and I don't know what this means. 他们给出了一个例子: weights=[10, 5, 30, 5] ,我不知道这意味着什么。 Why don't they sum to 100? 为什么他们总和不到100? If my population is [1, 2, 3, 4] -- does this mean that a choice of '10' occurs with probability 0.1? 如果我的人口是[1, 2, 3, 4] - 这是否意味着选择'10'的概率为0.1?

The total weight is 10+5+30+5=50. 总重量为10 + 5 + 30 + 5 = 50。

Suppose the population is [1,2,3,4]. 假设人口是[1,2,3,4]。

It returns 1 with probability 10/50 = 0.2 它以10/50 = 0.2的概率返回1

It returns 2 with probability 5/50 = 0.1 它返回2的概率为5/50 = 0.1

It returns 3 with probability 30/50 = 0.6 它以30/50 = 0.6的概率返回3

It returns 4 with probability 5/50 = 0.1 它以5/50 = 0.1的概率返回4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM