英文:
Random sample according to distribution
问题
如何根据概率分布从列表中返回多个唯一项目?
random.sample(population, k)
恰好是我想要的,但可惜它不接受概率分布。
random.choices(population, weights, k)
也几乎是我想要的,但它的输出可以包含来自群体的相同成员超过一次。
>>> random.choices([1, 2], [0.5, 0.5], k=2)
[1, 1]
对于这些参数,我希望输出始终是 [1, 2]
或 [2, 1]
。
英文:
How can I return multiple unique items from a list, according to a probability distribution?
random.sample(population, k)
is exactly what I want, but sadly it doesn't take a probability distribution.
random.choices(population, weights, k)
is also almost what I want, except its output can contain the same member of the population more than once
>>> random.choices([1, 2], [0.5, 0.5], k=2)
[1, 1]
For those arguments, I would like the output to always be [1, 2]
or [2, 1]
.
答案1
得分: 2
numpy.random.choice
可能是你想要的。你可以:
- 使用
replace=False
调用它以无替换方式进行抽样 - 通过参数
p
指定概率权重
英文:
numpy.random.choice
is probably what you want. You can:
- call it with
replace=False
to sample without replacement - specify probability weights through parameter
p
答案2
得分: 0
如@alexis_thual所提到的,请使用np.random.choice
。以下是一个示例,展示了它如何适用于你想要的情况:
import numpy as np
np.random.choice([1, 2], size=2, replace=False, p=[0.5, 0.5])
英文:
As mentioned by @alexis_thual, use np.random.choice
. Here is an example of how it would work for what you want:
import numpy as np
np.random.choice([1, 2], size=2, replace=False, p=[0.5, 0.5])
答案3
得分: -2
random.choices()函数位于Python的random模块中,允许你从一个给定的总体中生成一个带有权重的随机样本。然而,正如你提到的,它可能会在输出中产生重复的项目。
要生成一个根据概率分布的随机样本并避免重复项,你可以将random.choices()
与set()
结合使用,从结果中移除重复项。
以下是一个示例:
import random
population = [1, 2]
weights = [0.5, 0.5]
k = 2
sample = set(random.choices(population, weights, k))
result = list(sample)
# 如果样本中唯一项的数量少于k,重复这个过程直到达到要求
while len(result) < k:
sample = set(random.choices(population, weights, k - len(result)))
result.extend(list(sample))
print(result)
这段代码将根据给定的权重,从population
列表中生成一个包含k
个项目的随机样本。set()
函数用于从样本中移除重复项。如果样本中唯一项的数量少于k
,则代码会重复抽样的过程,直到有足够数量的唯一项为止。
希望对你有所帮助!
英文:
The random.choices() function in Python's random module allows you to generate a random sample from a population while specifying weights for each item. However, as you mentioned, it can produce duplicate items in the output.
To generate a random sample according to a probability distribution without duplicates, you can combine random.choices()
with set()
to remove duplicate items from the result.
Here is an example:
import random
population = [1, 2]
weights = [0.5, 0.5]
k = 2
sample = set(random.choices(population, weights, k))
result = list(sample)
# If the number of unique items in the sample is less than k, repeat the process until it matches
while len(result) < k:
sample = set(random.choices(population, weights, k - len(result)))
result.extend(list(sample))
print(result)
This code will generate a random sample of k
items from the population
list, based on the given weights
. The set()
function is used to remove any duplicate items from the sample. If the number of unique items in the sample is less than k
, the code repeats the sampling process until it has enough unique items.
Hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论