A histogram helps us see how data is spread out by counting how many values fall into different ranges.
0
0
Histogram computation with np.histogram() in NumPy
Introduction
You want to understand the distribution of exam scores in a class.
You need to check how many customers bought products in different price ranges.
You want to see the frequency of different ages in a survey.
You want to visualize how often certain temperature ranges occur in a month.
Syntax
NumPy
numpy.histogram(data, bins=10, range=None, density=False, weights=None, cumulative=False)
data: The list or array of numbers you want to analyze.
bins: How many groups (bars) you want to split your data into. Default is 10.
Examples
This splits the data into 3 groups and counts how many numbers fall into each group.
NumPy
counts, bin_edges = np.histogram([1, 2, 2, 3, 4, 5], bins=3)
Here, bins are set by specific edges: 0 to 2, 2 to 4, and 4 to 6.
NumPy
counts, bin_edges = np.histogram(data, bins=[0, 2, 4, 6])
This returns the probability density instead of counts, useful for comparing different sized datasets.
NumPy
counts, bin_edges = np.histogram(data, density=True)Sample Program
This code counts how many ages fall into 4 groups and shows the edges of these groups.
NumPy
import numpy as np # Sample data: ages of 10 people ages = np.array([22, 25, 27, 30, 22, 24, 29, 31, 35, 40]) # Compute histogram with 4 bins counts, bin_edges = np.histogram(ages, bins=4) print('Counts:', counts) print('Bin edges:', bin_edges)
OutputSuccess
Important Notes
The bin_edges array shows the boundaries of each group.
The number of counts is always one less than the number of bin edges.
If you want to plot the histogram, you can use matplotlib.pyplot.hist() which does this internally.
Summary
Histograms count how many data points fall into different ranges.
Use np.histogram() to get counts and bin edges separately.
You can control the number or exact edges of bins to see data distribution clearly.