NumPy | histogram method
Start your free 7-days trial now!
NumPy's histogram(~) method computes a histogram (frequency-count diagram).
Parameters
1. a | array-like
The input array.
2. binslink | array-like | optional
The desired number of bins. If an array is provided, then it must contain edges. By default, we get 10 equal-width bins.
3. rangelink | tuple of float | optional
By default, the range is set to (a.min(), a.max()).
4. weightslink | array-like | optional
An array containing the weights placed on each of the input values. If a value falls in a particular bin, instead of incrementing the count by one, we increment by the corresponding weight. The shape must be the same as that of a. By default, the weights are all one.
5. densitylink | boolean | optional
Whether to normalise to a probability density function (i.e. total area equaling one). By default, density=False.
Return value
A tuple of two NumPy arrays:
The values of the histogram (i.e. the frequency counts)
The bin edges
Examples
Basic usage
Here, the bin_edges represent the intervals of the bins, and the hist represents the number of values that fall between the interval. For instance, there is a total of one item that falls between the interval 1 and 1.9, so we get a value 1 for the spot in the histogram.
Specifying the number of bins
Specifying bin edges
Specifying a range
Specifying weights
The reason why we get a 5 there is that the two 6s we have in the input array obviously fall in the same bin, and since their respective weights are 4 and 5, we end up with a total bin count of 4+5=9.
Normalising