The numpy module of Python provides a function called numpy.histogram(). This function represents the frequency of the number of values that are compared with a set of values ranges. This function is similar to the hist() function of matplotlib.pyplot.
In simple words, this function is used to compute the histogram of the set of data.
numpy.histogram(x, bins=10, range=None, normed=None, weights=None, density=None)
x: array_like
This parameter defines a flattened array over which the histogram is computed.
bins: int or sequence of str or scalars(optional)
If this parameter is defined as an integer, then in the given range, it defines the number of equal-width bins. Otherwise, an array of bin edges which monotonically increased is defined. It also includes the rightmost edge, which allows for non-uniform bin widths. The latest version of numpy allows us to set bin parameters as a string, which defines a method for calculating optimal bin width.
range : (float, float)(optional)
This parameter defines the lower-upper ranges of the bins. By default, the range is (x.min(), x.max()). The values are ignored, which are outside the range. The ranges of the first element should be equal to or less than the second element.
normed : bool(optional)
This parameter is the same as the density argument, but it can give the wrong output for unequal bin widths.
weights : array_like(optional)
This parameter defines an array which contains weights and has the same shape as 'x'.
density : bool(optional)
If it is set to True, will result in the number of samples in every bin. If its value is False, the density function will result in the value of the probability density function in the bin.
hist: array
The density function returns the values of the histogram.
edge_bin: an array of float dtype
This function returns the bin edges (length(hist+1)).
import numpy as np a=np.histogram([1, 5, 2], bins=[0, 1, 2, 3]) a
Output:
In the above code
In the output, it shows a ndarray that contain the values of the histogram.
import numpy as np x=np.histogram(np.arange(6), bins=np.arange(7), density=True) x
Output:
import numpy as np x=np.histogram([[1, 3, 1], [1, 3, 1]], bins=[0,1,2,3]) x
Output:
import numpy as np a = np.arange(8) hist, bin_edges = np.histogram(a, density=True) hist bin_edges
Output:
import numpy as np a = np.arange(8) hist, bin_edges = np.histogram(a, density=True) hist hist.sum() np.sum(hist * np.diff(bin_edges))
Output:
In the above code
In the output, it shows a ndarray that contains the values of the histogram and the sum of histogram values.