Percentiles and Spread
Five-Number Summary
Describe a dataset's spread with five landmarks: minimum, Q1, median, Q3,
and maximum. With n=9, percentile positions (n−1)×p/100 for p∈{0,25,50,75,
100} land on exact integers (0, 2, 4, 6, 8) — no interpolation. Loop over
the five percents, compute each position, read sv[pos]. With numpy,
np.percentile(x, [0,25,50,75,100]) gives the same five values via linear
interpolation (exact at integer positions).
By hand
With the library
np.percentile(x, [0,25,50,75,100]) extends the same linear-interpolation
method to all five percentiles. Percentile 0 returns the minimum and 100
returns the maximum — no separate min()/max() calls needed. Values are
float; cast with int() to match.
naive.py
values = [4, 7, 2, 9, 1, 8, 5, 6, 3]
sv = sorted(values)
n = len(sv)
percents = [0, 25, 50, 75, 100]
summary = []
for p in percents:
pos = int((n - 1) * p / 100)
summary.append(sv[pos])
lo, q1, median, q3, hi = summary
print('RESULT:', (lo, q1, median, q3, hi))
library.py
import numpy as np
from dalib.display import set_display
set_display()
x = [4, 7, 2, 9, 1, 8, 5, 6, 3]
lo, q1, median, q3, hi = np.percentile(x, [0, 25, 50, 75, 100])
print('min:', int(lo))
print('Q1:', int(q1))
print('median:', int(median))
print('Q3:', int(q3))
print('max:', int(hi))
print('RESULT:', (int(lo), int(q1), int(median), int(q3), int(hi)))
min: 1
Q1: 3
median: 5
Q3: 7
max: 9
RESULT: (1, 3, 5, 7, 9)
Implementation notes
- The five-number summary is the data behind a box-and-whisker plot: the box spans Q1–Q3, the line inside is the median, and the whiskers extend to min and max (before outlier clipping).
np.percentilewith p=0 and p=100 is equivalent tomin()andmax(); calling them through percentile keeps the loop uniform and avoids two extra passes.- Cross-reference:
median-and-quartiles(this chapter) for the quartile position derivation;interquartile-range(this chapter) for the Q3−Q1 spread derived from this summary.