1

当我在脚本中运行以下内容时:

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]

我在这里想念什么?谢谢!

4

1 回答 1

1

那是因为你正在划分一个整数数组,你必须事先将它转换为浮点数:

>>> 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]
于 2013-06-16T08:17:16.550 回答