0
0
NumpyHow-ToBeginner ยท 3 min read

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 bins can be an integer or an array of edges.
  • Confusing density=True with 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

ParameterDescriptionDefault
dataInput array of valuesRequired
binsNumber of bins or bin edges array10
rangeLower and upper range of binsNone (min and max of data)
densityIf True, returns probability densityFalse
โœ…

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.