【发布时间】:2017-12-27 20:00:25
【问题描述】:
我想在 python 中模拟random.sample() 的功能,但是选择的分布不均匀(在这种情况下是三角形)。对此很重要的是,单个项目不会被选择两次(如 random.sample docs 中所述)。这是我所拥有的:
...
def tri_sample(population, k, mode=0):
"""
Mimics the functionality of random.sample() but with a triangular
distribution over the length of the sequence.
Mode defaults to 0, which favors lower indices.
"""
psize = len(population)
if k > psize:
raise ValueError("k must be less than the number of items in population.")
if mode > psize:
raise ValueError("mode must be less than the number of items in population.")
indices_chosen = []
sample = []
for i in range(k):
# This ensures unique selections
while True:
choice = math.floor(random.triangular(0, psize, mode))
if choice not in indices_chosen:
break
indices_chosen.append(choice)
sample.append(population[choice])
return sample
...
我怀疑这不是防止重复项目被拉出的理想方法。设计时我的第一个想法是复制population 和.pop() 的项目,因为它们被抽样以防止选择相同的项目两次,但我发现了两个问题:
- 如果
population是一个对象列表,则在复制该列表同时仍确保sample中的项目指向population中的相同对象时可能会遇到一些困难。 - 在总体上使用
.pop()会改变总体规模,每次都会改变分布。理想情况下,无论以何种顺序选择项目,分布(不确定我是否正确使用了该术语 - 每个项目被调用的概率)都是相同的。
有没有更有效的方法从总体中抽取非均匀随机样本?
【问题讨论】:
标签: python random probability