我需要写一个加权版的random。选择(列表中的每个元素有不同的被选择的概率)。这是我想到的:
def weightedChoice(choices):
"""Like random.choice, but each element can have a different chance of
being selected.
choices can be any iterable containing iterables with two items each.
Technically, they can have more than two items, the rest will just be
ignored. The first item is the thing being chosen, the second item is
its weight. The weights can be any numeric values, what matters is the
relative differences between them.
"""
space = {}
current = 0
for choice, weight in choices:
if weight > 0:
space[current] = choice
current += weight
rand = random.uniform(0, current)
for key in sorted(space.keys() + [current]):
if rand < key:
return choice
choice = space[key]
return None
这个函数对我来说太复杂了,而且很丑。我希望这里的每个人都能提供一些改进的建议或其他方法。对我来说,效率没有代码的整洁和可读性重要。
粗糙的,但可能足够:
import random
weighted_choice = lambda s : random.choice(sum(([v]*wt for v,wt in s),[]))
这有用吗?
# define choices and relative weights
choices = [("WHITE",90), ("RED",8), ("GREEN",2)]
# initialize tally dict
tally = dict.fromkeys(choices, 0)
# tally up 1000 weighted choices
for i in xrange(1000):
tally[weighted_choice(choices)] += 1
print tally.items()
打印:
[('WHITE', 904), ('GREEN', 22), ('RED', 74)]
假设所有权重都是整数。它们的和不一定是100,我这么做只是为了让测试结果更容易理解。(如果权重是浮点数,则将它们都乘以10,直到所有权重>= 1。)
weights = [.6, .2, .001, .199]
while any(w < 1.0 for w in weights):
weights = [w*10 for w in weights]
weights = map(int, weights)
为random.choice()提供一个预先加权的列表:
解决方案和测试:
import random
options = ['a', 'b', 'c', 'd']
weights = [1, 2, 5, 2]
weighted_options = [[opt]*wgt for opt, wgt in zip(options, weights)]
weighted_options = [opt for sublist in weighted_options for opt in sublist]
print(weighted_options)
# test
counts = {c: 0 for c in options}
for x in range(10000):
counts[random.choice(weighted_options)] += 1
for opt, wgt in zip(options, weights):
wgt_r = counts[opt] / 10000 * sum(weights)
print(opt, counts[opt], wgt, wgt_r)
输出:
['a', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'd', 'd']
a 1025 1 1.025
b 1948 2 1.948
c 5019 5 5.019
d 2008 2 2.008
这取决于你想对分布进行多少次抽样。
假设要对分布进行K次抽样。当n是分布中的项数时,每次使用np.random.choice()的时间复杂度为O(K(n + log(n)))。
在我的例子中,我需要对相同的分布进行多次采样,阶数为10^3其中n阶数为10^6。我使用了下面的代码,它预先计算了累积分布,并在O(log(n))中对其进行采样。总体时间复杂度为O(n+K*log(n))。
import numpy as np
n,k = 10**6,10**3
# Create dummy distribution
a = np.array([i+1 for i in range(n)])
p = np.array([1.0/n]*n)
cfd = p.cumsum()
for _ in range(k):
x = np.random.uniform()
idx = cfd.searchsorted(x, side='right')
sampled_element = a[idx]
在Udacity免费课程AI for Robotics中,Sebastien Thurn对此进行了演讲。基本上,他用mod运算符%做了一个权重索引的圆形数组,将变量beta设为0,随机选择一个索引,
for循环遍历N,其中N是指标的数量,在for循环中,首先按公式增加beta:
Beta = Beta +来自{0…2 * Weight_max}
然后在for循环中嵌套一个while循环per:
while w[index] < beta:
beta = beta - w[index]
index = index + 1
select p[index]
然后到下一个索引,根据概率(或课程中介绍的情况下的归一化概率)重新采样。
在Udacity上找到第8课,机器人人工智能的第21期视频,他正在讲粒子滤波器。