简体   繁体   中英

weird behavior of numpy.histogram / random numbers in numpy?

I stumbled upon some peculiar behavior of random numbers in Python , specifically I use the module numpy.random.

Consider the following expression:

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

In the limit of large N I would expect a binomial distribution (for the interested reader, this simulates the Ehrenfest model ) and for large n a normal distribution. A typical output however, looks like this:

array([
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
])

With the statement from above, I can't explain the occurrence of the zeros in the histogram - am I missing something obvious here?

You're using histogram wrong. The bins aren't where you think they are. They don't go from 0 to 50; they go from the minimum input value to the maximum input value. The 0s represent bins that lie entirely between two integers.

Try it with 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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM