The default for the bins
argument to np.histogram
is 10. So the histogram counts which bins your array elements fall into. In this case a = np.array([0, 1, 2, 3, 4])
. If we are creating a histogram with 10
bins then we break the interval 0-4 (inclusive) into 10 equal bins. This gives us (note that 11 end points gives us 10 bins):
np.linspace(0, 4, 11) = array([0. , 0.4, 0.8, 1.2, 1.6, 2. , 2.4, 2.8, 3.2, 3.6, 4. ])
We now just need to see which bins your elements in the array a
fall into. We can count them as follows:
[1, 0, 1, 0, 0, 1, 0, 1, 0, 1]
Now this is still not exactly what the output is. The density=True
argument states (from the docs): "If True
, the result is the value of the
probability density function at the bin, normalized such that
the integral over the range is 1."
Each bin (of height .5
) has a width of .4
so 5 x .5 x .4 = 1
as is the requirement of this argument.