Numpy.histogram – Python NumPy histogram Function

NumPy histogram Function:

Numpy.histogram: To compute the histogram of a set of data, use the NumPy histogram() function.

Python’s numpy module includes a function called numpy.histogram(). The frequency of the number of values compared with a set of value ranges is represented by this function. This function is comparable to matplotlib.pyplot hist() function.

Syntax:

numpy.histogram(a, bins=10, range=None, normed=None, weights=None, density=None)

Parameters

a: This is required. It specifies the input data, and the histogram will be calculated over the flattened array.

bins: This is optional. If this argument is specified as an integer, it specifies the number of equal-width bins in the specified range. Otherwise, an array of monotonically increasing bin edges is defined. It also has the rightmost edge, allowing for non-uniform bin widths. The most recent version of NumPy allows us to specify bin parameters as a string, defining a mechanism for computing optimal bin width. The default value is 10.

range: This is optional. It represents the lower and upper range for the bins. If range is not given, it is just (a.min(), a.max()). Outside of the range, values are ignored. The range’s first element must be less than or equal to the range’s second element.

normed: This is optional. This is similar to the density argument. But, for unequal bin widths, it may produce incorrect results. Since version 1.6.0, it has been deprecated.

weights: This is optional. It indicates an array of weights, of the same shape as “a”.

density: This is optional. If set to False, the result will include the number of samples in each bin. If True, the result is the probability density function value at the bin.

Return Value:

The values of the histogram are returned.

NumPy histogram Function in Python

Example1

Numpy histogram: The bins argument specifies the number of bins in the histogram, while the range parameter specifies the data range used to create the histogram.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random number to the arange() function to create an array from 0 to that number.
  • Store it in a variable.
  • Pass the above array as an argument to the histogram() function to get the histogram of it.
  • Store it in another variable.
  • (Here it creates 7 bins and each bin contains 7 elements except the last. It contains 8 elements).
  • Print the histogram of the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random number to the arange() function to create an array 
# from 0 to that number.
# Store it in a variable.
gvn_arry = np.arange(71)
# Pass the above array as an argument to the histogram() function
# to get the histogram of it.
# Store it in another variable.
arry_histgrm = np.histogram(gvn_arry)
# Here it creates 7 bins and each 
# bin contains 7 elements except the last. 
# It contains 8 elements.
# Print the histogram of the given array.
print("The histogram of the given array values:\n", arry_histgrm)

Output:

The histogram of the given array values:
(array([7, 7, 7, 7, 7, 7, 7, 7, 7, 8]), array([ 0., 7., 14., 21., 28., 35., 42., 49., 56., 63., 70.]))

Example2

The bins argument specifies the number of bins in the histogram, while the range parameter specifies the data range utilised to create the histogram.

Approach:

  • Import numpy module using the import keyword
  • Pass some random number to the arange() function to create an array
    from 0 to that number.
  • Store it in a variable.
  • Pass the above array, bins, range as the arguments to the histogram() function
    to get the histogram of it.
  • Store it in another variable.
  • Here it creates 6 bins for the range [0, 60]; each bin will have 10 items, and the last bin will have 11 elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random number to the arange() function to create an array 
# from 0 to that number.
# Store it in a variable.
gvn_arry = np.arange(71)
# Pass the above array, bins, range as the arguments to the histogram() function
# to get the histogram of it.
# Store it in another variable.
arry_histgrm = np.histogram(gvn_arry, bins=6, range=(0, 60))
# Here it creates 6 bins for the range [0, 60]; each bin will have 10 items,
# and the last bin will have 11 elements.
print("The histogram of the given array values:\n", arry_histgrm)

Output:

The histogram of the given array values:
    (array([10, 10, 10, 10, 10, 11]), array([ 0., 10., 20., 30., 40., 50., 60.]))

Example3

Here, the bins parameter is provided as a sequence with the density parameter. The density is set to True to acquire the probability density function at the bin, and the sequence defines the bin edges.

Approach:

  • Import numpy module using the import keyword
  • Pass some random number to the arange() function to create an array
    from 0 to that number.
  • Store it in a variable.
  • Give the random bins list as an argument to the array() function
    to create an array and store it in another variable.
  • Pass the above array, bins, desity values as the arguments to the histogram() function
    to get the histogram of it.
  • Store it in another variable.
  • Print the histogram of the given array values with a density False.
  • Print the histogram of the given array values with a density as True
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random number to the arange() function to create an array 
# from 0 to that number.
# Store it in a variable.
gvn_arry = np.arange(71)
# Give the random bins list as an argument to the array() function 
# to create an array and store it in another variable.
bins_arry = np.array([15, 25, 35, 45, 55])
# Pass the above array, bins, desity values as the arguments to the histogram() function
# to get the histogram of it.
# Store it in another variable.
arry_histgrm_1 = np.histogram(gvn_arry, bins=bins_arry, density=False)
arry_histgrm_2= np.histogram(gvn_arry, bins=bins_arry, density=True)
# Print the histogram of the given array values with density as False
print("The histogram of the given array values with density as False:")
print(arry_histgrm_1)
# Print the histogram of the given array values with density as True
print("The histogram of the given array values with density as True:")
print(arry_histgrm_2)

Output:

The histogram of the given array values with density as False:
(array([10, 10, 10, 11]), array([15, 25, 35, 45, 55]))
The histogram of the given array values with density as True:
(array([0.02439024, 0.02439024, 0.02439024, 0.02682927]), array([15, 25, 35, 45, 55]))

Example4

# Import numpy module using the import keyword
import numpy as np
# Import pyplot from the matplotlib module using the import keyword
import matplotlib.pyplot as plt
# Pass some random number to the arange() function to create an array 
# from 0 to that number.
# Store it in a variable.
gvn_arry = np.arange(130)
# Give the random bins list as an argument to the array() function 
# to create an array and store it in another variable.
bins_arry = np.array([15, 20, 35, 45, 65, 85, 95, 105])
# Pass the above array, bins array, edge color values as the arguments to the hist() function
# to get the histogram of it and plot it using the matplotlib module .
plt.hist(gvn_arry, bins = bins_arry, edgecolor='red') 
plt.title("Plotting Histogram") 
plt.show()

Output:

Plotting Histogram