在numpy中numpy.histogram /随机数的奇怪行为?

ram*_*ler 0 python random statistics numpy

我在Python中偶然发现了随机数的一些特殊行为,特别是我使用模块numpy.random.

请考虑以下表达式:

n = 50
N = 1000
np.histogram(np.sum(np.random.randint(0, 2, size=(n, N)), axis=0), bins=n+1)[0]
Run Code Online (Sandbox Code Playgroud)

在大的限制中,N我期望二项分布(对于感兴趣的读者,这模拟Ehrenfest模型)和大n的正态分布.然而,典型的输出看起来像这样:

阵列([
1,0,0,1,0,2,0,1,0,15,0,
12,0,18,0,39,0,64,0,62,0,109,
0,97 ,0,107,0,114,0,102,0,92,0,
55,0,46,0,35,0,10,0,9,0,4,
0,0,0,3,0 ,1,1
]]

根据上面的陈述,我无法解释直方图中零点的出现 - 我在这里遗漏了一些明显的东西吗?

use*_*ica 5

histogram错了.垃圾箱不在你认为的地方.它们不会从0到50; 它们从最小输入值到最大输入值.0表示完全位于两个整数之间的区间.

尝试使用numpy.bincount:

In [31]: n = 50

In [32]: N = 5000

In [33]: np.bincount(np.sum(np.random.randint(0, 2, size=(n, N)), axis=0))
Out[33]: 
array([  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   7,  13,  22,  46,  75, 126, 220, 305, 367, 461, 550, 578,
       517, 471, 438, 314, 189, 146,  76,  50,  17,   9,   2,   1])
Run Code Online (Sandbox Code Playgroud)