# How numpy.histogram() function works?

Learn, about the numpy.histogram() function, its usages, and example. How does numpy.histogram() function work?
Submitted by Pranit Sharma, on March 04, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

## Python - numpy.histogram() Function

numpy.histogram() is used to compute the histogram of a dataset. A histogram is an approximate representation of the distribution of numerical data.

It takes a parameter bins that defines the number of equal-width bins in the given range (10, by default). If bins are a sequence, it defines a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform bin widths.

### Syntax

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

### Parameter(s)

• a: Input data. The histogram is computed over the flattened array.
• bins: If binsis an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform bin widths.
• range: The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. rangeaffects the automatic bin computation as well. While bin width is computed to be optimal based on the actual data within range, the bin count will fill the entire range including portions containing no data.

Let us understand with the help of an example,

## Python program to demonstrate the example of numpy.histogram() function

```# Import numpy
import numpy as np

# Creating a numpy array
arr = np.array([1, 2, 1])

# Display original array
print("Original Array:\n",arr,"\n")

# Creating bins
bins = [0, 1, 2, 3]

# Computing histogram
res = np.histogram(arr,bins)

# Display result
print("Result:\n",res)
```

Output:

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates