【发布时间】:2019-11-12 17:08:40
【问题描述】:
我正在根据一组概率probs 上的多项分布生成一个抽奖向量,其中每个抽奖都是probs 中所选条目的索引:
import numpy as np
def sample_mult(K, probs):
result = np.zeros(num_draws, dtype=np.int32)
for n in xrange(K):
draws = np.random.multinomial(1, probs)
result[n] = np.where(draws == 1)[0][0]
return result
这可以加速吗?一遍又一遍地调用np.random.multinomial 似乎效率低下(而且np.where 也可能很慢。)
timeit 说The slowest run took 6.72 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 18.9 µs per loop
【问题讨论】:
标签: python numpy optimization scipy vectorization