当我在脚本中运行以下内容时:
from pylab import *
N_rec = 1000
pt = 0.1
bitstrm = rand(N_rec,1)
bitstrm = (bitstrm<=pt)
hist_strm = histogram(bitstrm, 2)
p_strm = (hist_strm[0])/sum(hist_strm[0])
print p_strm
我得到[0 0]
但是,在控制台上执行此操作将返回正确答案:
[ 0.913 0.087]
我在这里想念的是什么? 谢谢!
答案 0 :(得分:1)
那是因为你要划分一个整数数组,你必须先将其转换为float:
>>> from pylab import *
>>> import numpy as np
>>> N_rec = 1000
>>> pt = 0.1
>>> bitstrm = rand(N_rec,1)
>>> bitstrm = (bitstrm<=pt)
>>> hist_strm = histogram(bitstrm, 2)
>>> p_strm = (hist_strm[0]).astype(np.float)/sum(hist_strm[0])
>>> print p_strm
[ 0.894 0.106]