Inspect a Distribution
Count values inside explicit intervals to inspect concentration, spread, gaps, and unusual values while remembering that a histogram discards order.
A list of residuals records how far individual measurements were from a reference value. Sometimes their order matters. Here our question is different: where do the residuals concentrate, how widely do they spread, and does any value sit apart from the rest? A histogram answers by counting values inside stated intervals.
We will use twelve small residuals, in degrees Celsius:
The values are numerous enough to count in groups, but still few enough to check by hand.
Count Values before Drawing Bars
A histogram needs bin edges. These edges define the intervals whose counts become bar heights:
[1 4 5 1 1]
[-1.5 -0.5 0. 0.5 1. 1.5]
12
The six edges form five bins. NumPy places a boundary value in the bin that starts at that boundary, except that the last bin includes its final right edge. The hand count is:
| Interval | Values | Count |
|---|---|---|
[-1.5, -0.5) | -0.8 | 1 |
[-0.5, 0.0) | -0.4, -0.3, -0.2, -0.1 | 4 |
[0.0, 0.5) | 0.0, 0.1, 0.2, 0.3, 0.4 | 5 |
[0.5, 1.0) | 0.8 | 1 |
[1.0, 1.5] | 1.4 | 1 |
The counts sum to 12, the number of residuals. That conservation check can
catch edges that fail to cover part of the intended range.
Q1. Count one explicit bin
How many residuals lie in the interval [-0.5, 0.0)?
Compute it first, then check your number.
HintApply both interval boundaries
Count values that are at least -0.5 and strictly below 0.0.
SolutionFour negative residuals occupy the bin
The values are -0.4, -0.3, -0.2, and -0.1, so the count is 4.
Draw the Same Counts
Use the same explicit edges for the numerical count and the plot:
Count and plot residuals
The printed counts are the numerical evidence behind the five bars. Change one residual across an edge and compare the count change with the plot.
Ready to run.
The bars near zero are tallest: four values lie just below zero and five lie
from zero through below 0.5. One value lies in each outer positive bin. The
histogram no longer tells us which observation produced a value because it
groups by numerical interval and discards observation order.
The residual 1.4 is separated from most of this small sample. That is an
observation, not an explanation. The plot does not tell us whether the value
comes from an unusual measurement, a faulty sensor, or ordinary variation.
Q2. Move a value across an edge
Suppose 0.4 changes to 0.6 while the edges stay fixed. Which two bin counts
change?
Select one choice, then check.
HintPlace the old and new values
0.4 is below the 0.5 edge; 0.6 is at least 0.5 and below 1.0.
SolutionTransfer one count between adjacent bins
The third count decreases by one and the fourth increases by one. The total
count remains 12.
Use Shared Edges for a Fair Comparison
Two histograms are directly comparable only when their bins mean the same intervals. Consider residuals from a second sensor:
Because both calls use bin_edges, the third count means [0.0, 0.5) in
both arrays. If each plot selected its own edges, bars at the same horizontal
position could represent different intervals.
A small histogram is sensitive to individual values and edge choices. With twelve values, moving one value changes a bar by one count, which can be a large fraction of its height. Describe what these values show, but do not infer a stable population shape from this sample alone.
Q3. Count two samples with shared bins
Complete the program so both residual arrays use the declared shared edges.
Editable Python
Ready to run.
HintKeep the first return value
np.histogram(values, bins=shared_edges) returns counts and edges. Unpack
each call as counts, _.
SolutionReuse one edge array
The counts are comparable bin by bin because every position refers to the same interval.
A histogram turns explicit numerical intervals into counts and discards observation order. Count small data by hand, reuse edges for comparison, and treat patterns from a small sample cautiously. The next lesson preserves two array axes and maps each table value to color.