+5 votes
in Programming Languages by (40.5k points)
In Python, how can I find the number of elements in each bin of a histogram?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

The histogram() function of the Numpy module computes the histogram of a dataset. It returns bin edges and the value of the histogram. The value of the histogram represents the number of elements in each bin.

Here is an example:

>>> import numpy as np
>>> import random
>>> a=[random.random() for _ in range(15)]
>>> a
[0.8371601421970039, 0.7511725216866857, 0.4424049452497366, 0.2877455161859661, 0.7632456353300519, 0.8196282040760022, 0.6400867612568486, 0.8548527042441426, 0.12257666317541871, 0.8296549025802453, 0.13648784817663995, 0.1838239540293889, 0.5173459297717008, 0.19341387676836408, 0.5716937417929663]
>>> bins = [0, 0.3, 0.6, 0.9, 1]
>>> np.histogram(a,bins)
(array([5, 3, 7, 0]), array([0. , 0.3, 0.6, 0.9, 1. ]))
>>> v, e = np.histogram(a,bins)
>>> v
array([5, 3, 7, 0])
>>> e
array([0. , 0.3, 0.6, 0.9, 1. ])

In this example, I have randomly generated 15 numbers between 0 and 1 and have created 4 bins: 0-0.3, 0.3-0.6, 0.6-0.9, 0.9-1.

The histogram() function returned two arrays: the bins I provided, and the other is the number of elements in those bins. E.g., there are 5 elements of the list in the range 0-0.3, and there are 5 elements in the range 0.6-0.9.

If you mention the number of bins you want to create, the histogram() function will automatically determine the range for those bins.

In the below example, I want to create two bins. The function automatically selected the bin edges and computed the number of elements in those bins.

>>> v, e = np.histogram(a,bins=2)
>>> v
array([6, 9])
>>> e
array([0.12257666, 0.48871468, 0.8548527 ])
>>>


...