我有一个遵循泊松分布的数字数组,我想覆盖每个非零值本身乘以某个随机数
from scipy import stats
import numpy as np
jumps = stats.poisson(mu=0.05).rvs(200) # generate 200 random poisson numbers
idx = np.where(jumps!=0)[0] # get the index where jumps is non zero
for i in idx:
# want to overwrite the i'th index in jumps with itself
# multiplied by a random float
jumps[i] = jumps[i]*np.random.normal()
print(jumps) # we get only 0,-1,1
>>> [0 0 -1 ....
0 0 0]
即使我包含一个临时变量,我仍然只能得到0,-1,1。
for i in idx:
# want to overwrite the i'th index in jumps with itself
# multiplied by a random float
tmp = jumps[i]*np.random.normal()
jumps[i] = tmp
print(jumps) # we still get only 0,-1,1
>>> [0 0 -1 ....
0 0 0]
如果在每次迭代中打印出tmp
,您会发现它工作正常。但是由于某些原因,jumps[i]
没有被正确覆盖。
这是怎么回事?