Percentiles — Where Do You Stand?
Percentiles answer one powerful question: what percentage of the data falls below this value?
The 75th percentile means 75% of values are lower. Use NumPy's percentile() to compute any percentile.
EXERCISE
1What Are Percentiles?
Percentiles answer one specific question: what percentage of the data falls below this value?
When a doctor says your child is in the 75th percentile for height, they mean your child is taller than 75% of kids their age. The child is not "75% tall" — they are taller than 75% of the group. That is a percentile.
How it works:
import numpy
ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
# What age is the 75th percentile?
p75 = numpy.percentile(ages, 75)
print(p75)
# Output: 43.0
# Meaning: 75% of people are 43 or younger
Common percentiles have special names:
| Percentile | Name | Meaning |
|---|---|---|
| 25th | Q1 (First Quartile) | 25% of data is below this |
| 50th | Q2 (Median) | 50% below — the middle value |
| 75th | Q3 (Third Quartile) | 75% of data is below this |
| 90th | P90 | Only 10% of data is above this |
import numpy
ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
print(f"25th: {numpy.percentile(ages, 25)}")
# Output: 25th: 10.0
print(f"50th: {numpy.percentile(ages, 50)}")
# Output: 50th: 31.0
print(f"90th: {numpy.percentile(ages, 90)}")
# Output: 90th: 61.0
Pro Tip: The 50th percentile is just the median by another name. Once you realise this, percentiles stop feeling exotic — they are just a generalisation of the median concept to any split point, not just 50%.
EXERCISE
2Using Percentiles to Understand Your Data
Percentiles give you a complete picture of your data's shape — from the bottom 10% to the top 1%.
If the mean is a single photo of your data, percentiles are a panorama. They show you the full landscape — where most values cluster, where the tails stretch, and where the outliers hide.
Building a percentile profile:
import numpy
ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
for p in [10, 25, 50, 75, 90]:
print(f"P{p}: {numpy.percentile(ages, p)}")
# Output:
# P10: 5.8
# P25: 10.0
# P50: 31.0
# P75: 43.0
# P90: 61.0
The IQR — Interquartile Range:
import numpy
ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
q1 = numpy.percentile(ages, 25)
q3 = numpy.percentile(ages, 75)
iqr = q3 - q1
print(f"Q1: {q1}, Q3: {q3}, IQR: {iqr}")
# Output: Q1: 10.0, Q3: 43.0, IQR: 33.0
# The middle 50% of ages spans 33 years
Detecting outliers with percentiles:
import numpy
ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
q1 = numpy.percentile(ages, 25)
q3 = numpy.percentile(ages, 75)
iqr = q3 - q1
lower = q1 - 1.5 * iqr
upper = q3 + 1.5 * iqr
outliers = [x for x in ages if x < lower or x > upper]
print(f"Outliers: {outliers}")
# Output: Outliers: [80, 82]
Pro Tip: The IQR method (values below Q1 - 1.5×IQR or above Q3 + 1.5×IQR are outliers) is more robust than using the mean ± 2σ because it does not assume your data is normally distributed. This is the method behind every box plot you have ever seen.
EXERCISE
3Percentiles in the ML Pipeline
From clipping outliers to evaluating model confidence, percentiles are a quiet workhorse in every ML pipeline.
Percentiles in ML are like guardrails on a highway — they keep your data on the road and flag anything flying off the edges.
Use Case 1: Clipping outliers
import numpy
prices = [10, 15, 20, 25, 30, 35, 500]
p95 = numpy.percentile(prices, 95)
clipped = [min(x, p95) for x in prices]
print(f"P95: {p95}")
# Output: P95: 215.0
print(f"Clipped: {clipped}")
# Output: Clipped: [10, 15, 20, 25, 30, 35, 215.0]
# The extreme 500 is capped at the 95th percentile
Use Case 2: Feature binning
import numpy
ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31]
bins = numpy.percentile(ages, [25, 50, 75])
print(f"Bin boundaries: {bins}")
# Output: Bin boundaries: [10. 31. 43.]
# Now you can label: "young" (<10), "adult" (10-43), "senior" (>43)
Use Case 3: Confidence in predictions
import numpy
# Multiple model predictions for the same input
predictions = numpy.random.normal(100, 5, 1000)
p5 = numpy.percentile(predictions, 5)
p95 = numpy.percentile(predictions, 95)
print(f"90% confidence interval: [{round(p5,1)}, {round(p95,1)}]")
# Output: 90% confidence interval: [~91.8, ~108.2]
Pro Tip: When you see a prediction like "the house price is $340,000 with a 90% confidence interval of [$310K, $370K]" — those boundaries are just the 5th and 95th percentiles of the model''s prediction distribution. Percentiles turn a single guess into a range of trustworthy estimates.
