我正在编写癌细胞群体增长的模拟,我正在使用numpy.random函数来模拟获得或失去突变的细胞。我已经通过分析确定代码中的瓶颈(大约70%的运行时)是包含numpy.random函数的前几行。这里变量num_steps
是一个大数字,大约一百万:
def simulate(mu, gamma, beta, num_steps, threshold):
mutation_num = 0 # the index of the mutation (we assume each mutation only occurs once)
population = {() : 1} # represents population: tuple of mutations and number of cells with those mutations
for epoch in range(num_steps):
next_population = {}
for mutations, size in population.items():
born = np.random.binomial(size, birth_rate)
if np.random.binomial(born, gamma):
return True
mut_loss = 0 # initializing in case variable is not created
if mutations:
mut_gain, mut_loss, mut_same = np.random.multinomial(born, [mu, beta, 1-mu-beta])
else:
mut_gain, mut_same = np.random.multinomial(born, [mu, 1-mu])
.....
有没有办法让np.random.binomial
和np.random.multinomial
函数运行得更快?我尝试过使用Cython,但这并没有帮助。
答案 0 :(得分:2)
说明我的评论:
In [81]: timeit np.random.binomial(1,1,1000)
46.4 µs ± 1.53 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [82]: %%timeit
...: for _ in range(1000):
...: np.random.binomial(1,1)
...:
4.77 ms ± 186 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
如果可能的话,一次调用而不是一次调用生成许多随机值。