# np.histogram: How does numpy historgam() works in Python

0
178

The np.histogram() is a numpy library function that returns an array that can be used for plotting in the graph. The array is created based on the parameters passed. The np.histogram() function computes the histogram for the data given inside the function. It can be used for exploring the data. This array can be plotted in a graph to easily understand the data.

## Syntax

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

## Arguments

The np.histogram() function takes one required argument as a parameter and has five optional arguments:

1. arr: The array is passed in this argument. This array is the required argument for returning the histogram. The array to be given to the histogram function is shown in this argument.
2. bins: It is the number of bins. Ten is kept as the default bins. This bin’s value can be a sequence of numbers or int or str. If this is int, then the width of the bins is equally created. If it is a list, then the values for the bins changes.
3. range: This is the range within which the values will be considered. If the value exceeds the range then that value will not be considered. This range consists of two values, start, and end. This is enclosed within a tuple. This tuple consists of ( start, end ) start and end values in the range.
4. normed: This is similar to the density argument.
5. weight: This argument is passed with an array consisting of weights. This weight array’s shape should be equal to the shape of the array a. These weights are normalized if the density argument is set to True
6. density: If it is True, then it is a value of the probability density function. If it is False, the array will have the number of samples present in each bin.

## Return value

It returns two values. One is an array consisting of histogram values, and the other consists of bin edges.

## Python program for returning the histogram array using np.histogram()

``````# Importing numpy as np
import numpy as np

# Creating a numpy array called arr
arr = np.array([5, 3, 7, 8, 1, 9, 2])
print("The array is : ", arr)

# Printing the shape of the array using shape function
print(" Shape of the array is : ", arr.shape)
res = np.histogram(arr)
print("The Histogram array is: ", res)``````

#### Output

``````The array is : [5 3 7 8 1 9 2]
Shape of the array is : (7,)
The Histogram array is: (array([1, 1, 1, 0, 0, 1, 0, 1, 1, 1]), array([1. , 1.8, 2.6, 3.4, 4.2, 5. , 5.8, 6.6, 7.4, 8.2, 9. ]))/* Your code... */``````

In this program, we imported numpy to create a numpy array. Then, we printed the array and printed the shape of the array using the np.shape.

We passed this array into the np.histogram() function in the next step. The np.histogram() function returns two values one is an array consisting of histogram values, and the other is the bins edge values.

## Python program for returning the histogram array for random numbers using np.histogram()

``````# Importing numpy as np
import numpy as np

# Creating a numpy array called arr
arr = np.random.randint(70, size=(40))
print("The array is : ", arr)

# Printing the shape of the array using shape function
print(" Shape of the array is : ", arr.shape)
res = np.histogram(arr, bins=[0, 10, 20, 30, 40, 50], range=(0, 50))
print("The Histogram array is: ", res)``````

#### Output

``````The array is : [41 19 64 35 10 60 56 56 6 28 37 61 8 4 39 45 29 3 5 19 24 28 57 10
30 46 51 30 16 40 57 64 54 68 37 30 2 41 41 12]
Shape of the array is : (40,)
The Histogram array is: (array([6, 6, 4, 7, 6]), array([ 0, 10, 20, 30, 40, 50]))``````

In this program, we passed the values bins and range. The range() function removes the values greater than and less than the specified range. Only the values that are within the specified range will be considered.

That’s it for this tutorial.

## Related posts

np.argsort

np.count

np.vstack

np.diff

np.unique

This site uses Akismet to reduce spam. Learn how your comment data is processed.