How to Use np.histogram in NumPy: Syntax and Examples
Use
np.histogram to compute the histogram of data by specifying the data array and optionally the number of bins or bin edges. It returns the counts of data points in each bin and the bin edges as two arrays.Syntax
The basic syntax of np.histogram is:
np.histogram(data, bins=10, range=None, density=False, ...)
Where:
- data: The input array of values to bin.
- bins: Number of equal-width bins or an array defining bin edges.
- range: Tuple specifying the lower and upper range of bins.
- density: If True, returns probability density instead of counts.
python
np.histogram(data, bins=10, range=None, density=False)
Example
This example shows how to compute a histogram of random data with 5 bins and print the counts and bin edges.
python
import numpy as np # Generate 100 random numbers between 0 and 10 data = np.random.uniform(0, 10, 100) # Compute histogram with 5 bins counts, bin_edges = np.histogram(data, bins=5) print('Counts:', counts) print('Bin edges:', bin_edges)
Output
Counts: [22 21 19 19 19]
Bin edges: [ 0. 2. 4. 6. 8. 10. ]
Common Pitfalls
Common mistakes when using np.histogram include:
- Not understanding that
binscan be an integer or an array of edges. - Confusing
density=Truewith counts; it returns probability density, not counts. - Ignoring that the last bin edge is inclusive on the right but exclusive on the left except for the last bin.
Example of a wrong and right way:
python
# Wrong: expecting density=True to return counts import numpy as np data = np.array([1, 2, 2, 3, 4]) counts, edges = np.histogram(data, bins=3, density=True) print('With density=True:', counts) # Right: density=False returns counts counts, edges = np.histogram(data, bins=3, density=False) print('With density=False:', counts)
Output
With density=True: [0.25 0.5 0.25]
With density=False: [1 2 2]
Quick Reference
| Parameter | Description | Default |
|---|---|---|
| data | Input array of values | Required |
| bins | Number of bins or bin edges array | 10 |
| range | Lower and upper range of bins | None (min and max of data) |
| density | If True, returns probability density | False |
Key Takeaways
np.histogram returns counts and bin edges for data distribution.
Specify bins as an integer or array of edges to control binning.
Use density=True to get probability density instead of counts.
The last bin includes the right edge; others exclude it.
Always check output to understand binning and counts.