ML Percentile
ML Percentiles and Quartiles: Understanding Distribution Tails
A percentile tells you the threshold below which a given percentage of observations fall. For example, if a server's 95th percentile (p95) response time is 180 ms, then 95% of requests finished in 180 ms or less while the slowest 5% took longer.
Percentiles give ML engineers and backend teams an honest view of tail behavior that averages completely hide.
Computing Quantiles and Percentiles in Python
Python 3.8+ includes statistics.quantiles() for splitting data into quartiles (n=4) or percentiles (n=100). In NumPy, np.percentile(data, [25, 50, 75, 95]) computes multiple thresholds at once.
import statistics
latencies_ms = [42, 48, 51, 55, 60, 63, 70, 82, 110, 295]
q1, median, q3 = statistics.quantiles(latencies_ms, n=4)
iqr = q3 - q1
upper_fence = q3 + 1.5 * iqr
print(f"Q1={q1}, Median={median}, Q3={q3}")
print(f"IQR={iqr}, Upper outlier threshold={upper_fence}")
Detecting Outliers with the IQR Method
Unlike mean and standard deviation, the Interquartile Range (IQR = Q3 - Q1) resists distortion from extreme spikes. Any point above Q3 + 1.5 * IQR or below Q1 - 1.5 * IQR is a classic box-plot outlier candidate.
Common Mistakes to Avoid
- Deleting every IQR outlier automatically: High values like
$5,000purchases or295 msspikes may be real fraud or timeout events your model needs to learn. - Confusing the 50th percentile with the mean: The 50th percentile is always the median, not the arithmetic mean.
- Reporting only average latency: An average of
60 mscan hide ap99spike of3 secondsaffecting real users.
Frequently Asked Questions
What is the difference between a percentile and a quantile?
Percentiles divide data into 100 equal parts (0–100), whereas quantiles generalize that idea to any count, such as quartiles (4 parts) or deciles (10 parts).
How do I calculate p99 with NumPy?
Call np.percentile(latencies, 99) (or np.quantile(latencies, 0.99)).
What is a box plot showing?
A box plot draws Q1, the median (Q2), and Q3 as the box, with whiskers extending to the 1.5 * IQR fences and dots marking outliers beyond them.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.