Statistics for machine learning
Mean, median, std, and percentiles - the numbers you report before you train.
Why it matters: Median vs mean shows skew. p95 latency is what SLAs use. Averages alone hide problems.
Basics
Mean, median, mode
Mean is the average. One outlier can pull it far.
Median is the middle value when sorted. It ignores extremes.
Mode is the most common value. It works on labels too.
If mean and median disagree a lot, the data is skewed. Quote the median.
Tiny example
Compare mean and median on a tiny latency list.
scores = [120, 130, 140, 9000]
mean = sum(scores) / len(scores)
median = sorted(scores)[len(scores) // 2]
print("mean", mean, "median", median)Basics
Variance and standard deviation
Standard deviation is spread in the original units.
Small std means values cluster. Large std means they scatter.
NumPy np.std defaults to population (ddof=0). Sample std uses ddof=1.
In ML, standardisation is (x - mean) / std.
Tiny example
Compute sample std with NumPy ddof=1.
import numpy as np
x = np.array([0.9, 0.8, 0.7, 0.95])
print(float(np.std(x, ddof=1)))Basics
Percentiles and outliers
A percentile is the value below which that share of the data falls. Median is p50.
Latency reports use p50, p95, and p99 because the tail is what users feel.
IQR is p75 - p25. A common outlier fence is 1.5 * IQR beyond p25 or p75.
Tiny example
Print p50 and p95 for a tiny latency array.
import numpy as np
lat = np.array([100, 120, 140, 180, 900])
print("p50", np.percentile(lat, 50), "p95", np.percentile(lat, 95))Then AI / ML
AI / ML examples
Same idea, used in real AI work. Press Try in lab to run it.
AI / ML example
AI / ML: mean vs median with a timeout
One slow call wrecks the mean; the median barely moves.
In AI / ML: API SLAs quote p50/p95. One timeout should not rewrite the story.
example.py
Python
import statistics
latencies = [180, 210, 195, 205, 190, 30000]
print("mean", round(statistics.mean(latencies), 1))
print("median", statistics.median(latencies))
print("without timeout mean", round(statistics.mean(latencies[:-1]), 1))AI / ML example 2
AI / ML: IQR outlier fence
Flag values outside 1.5 x IQR.
In AI / ML: Feature cleaning and anomaly checks often start with an IQR rule.
example.py
Python
import numpy as np
values = np.array([12, 14, 13, 15, 12, 99, 14, -40], dtype=float)
q1, q3 = np.percentile(values, [25, 75])
iqr = q3 - q1
low, high = q1 - 1.5 * iqr, q3 + 1.5 * iqr
mask = (values < low) | (values > high)
print("outliers", values[mask].tolist())
print("clean", values[~mask].tolist())Takeaways
- 1Quote median when outliers exist; report mean and median together.
- 2NumPy std defaults to ddof=0; use ddof=1 for sample std.
- 3Report p95/p99 for latency; use 1.5 x IQR to spot outliers.