Histograms
Intuition
A histogram groups numbers into bins (intervals) and counts how many fall in each — a quick picture of a distribution's shape: where it peaks, how wide it is, whether it is skewed.
Precise explanation
With range and bins, width and edges
for . Bin is the half-open interval ,
except the last bin, which is closed so the maximum value is counted.
This is the same convention NumPy's np.histogram uses.
Index of value : , then clamp to . Values below lo or above hi are ignored.
If lo/hi are not given, use the data's min and max. If they are equal, the width is zero; a common convention (used here) is to widen the range to .
Figure (original): counts per bin; the last bin includes its right edge.
Worked example
data [1, 2, 2, 3, 4, 5], 2 bins over [1, 5]: width 2, edges [1, 3, 5].
Bin 0 = [1, 3): 1, 2, 2 → 3. Bin 1 = [3, 5]: 3, 4, 5 → 3.
Common mistakes
- Dropping the maximum value because every bin is half-open.
- Floating-point edges such as 0.1 + 0.2; compute edges as
lo + j*w. - Too few/too many bins hiding the shape.
Why this matters
Histograms are the first look at any feature, the basis of intensity histograms in images (CV-09), and the binning used in calibration plots.
Library implementation
np.histogram(data, bins=k, range=(lo, hi)) uses the same rule: equal-width bins, each
half-open [a, b) except the last, which also includes hi; when lo == hi it widens the range
by 0.5 on each side, exactly as this exercise specifies. It returns (counts, edges) — the
opposite order to your function.
References
- Think Stats (3rd edition) — Allen B. Downey (
S010) - NumPy: the absolute basics for beginners (NumPy v2.5 Manual) — NumPy Developers (
S007)