Mean balances all values equally—including extreme ones. The median answers a different question: what is the middle value when data is sorted? For skewed distributions common in AI workloads (latency, revenue, error magnitudes), the median often represents “typical” better than the mean.
Robust statistics matter in production ML: a median imputation strategy survives outliers; median absolute deviation (MAD) complements standard deviation for contaminated data. This lecture pairs with Mode to complete the trio of central tendency measures before Variance quantifies spread.
Learning Objectives
By the end of this lesson, students should be able to:
- Compute the median for odd- and even-length datasets.
- Interpret the median as the 50th percentile.
- Compare mean and median on symmetric vs skewed distributions.
- Apply median imputation for missing values in preprocessing pipelines.
- Explain why the median minimizes absolute deviation (L1 sense).
- Use medians in robust statistics (MAD, median filter in image processing).
- Recognize when reporting median vs mean affects model evaluation metrics.
- Identify common misconceptions about medians and ordinal vs numeric data.
Introduction: The Middle Value
Sort your data from smallest to largest. The median is the value at the center—half the observations fall below it, half above. Unlike the mean, it ignores how far extreme values sit from the center; only their rank matters.
In ML engineering, medians appear when means mislead: API latency reports use p50 (median) alongside p95 and p99; housing price benchmarks cite median price because a few mansions inflate the mean; robust scalers use median and IQR instead of mean and standard deviation.
For sorted data x(1) ≤ x(2) ≤ … ≤ x(n):
- Odd n: Median = x((n+1)/2) — the middle element.
- Even n: Median = (x(n/2) + x(n/2 + 1)) / 2 — average of the two middle elements.
Equivalently, the median is the 50th percentile: the value below which 50% of observations fall.
| Dataset | Sorted Values | Median | Mean (for contrast) |
|---|---|---|---|
| Odd count: {3, 1, 9} | {1, 3, 9} | 3 | 4.33 |
| Even count: {2, 4, 6, 8} | {2, 4, 6, 8} | 5 | 5 |
| With outlier: {10, 12, 11, 1000} | {10, 11, 12, 1000} | 11.5 | 258.25 |
Mean
- Uses all values numerically
- Sensitive to outliers
- Minimizes sum of squared errors
- Best for symmetric, light-tailed data
Median
- Uses rank (position in sorted order)
- Robust to outliers
- Minimizes sum of absolute errors
- Best for skewed or heavy-tailed data
Median and L1 Loss
Just as the mean minimizes squared deviation, the median minimizes absolute deviation: it is the value m that minimizes ∑|xi − m|. This connects to L1 loss (mean absolute error, MAE) in regression—MAE predictions converge toward conditional medians rather than conditional means. Choosing MSE vs MAE is choosing mean vs median optimality.
Production SLAs track p50 (median), p95, and p99 response times. The median tells you the typical user experience; tail percentiles catch worst-case degradation. Reporting only the mean latency hides that 5% of users may wait ten times longer.
Median in Preprocessing
| Technique | Median Role | Why Not Mean? |
|---|---|---|
| Median imputation | Fill missing values with column median | Outliers do not distort imputed value |
| RobustScaler (sklearn) | Center using median, scale by IQR | Resistant to extreme feature values |
| Median filter (images) | Replace pixel with neighborhood median | Removes salt-and-pepper noise while preserving edges |
Reality: The mean uses more information (magnitudes, not just ranks) and has nicer algebraic properties for theory and gradient-based optimization. Use median for reporting and robust preprocessing; use mean for differentiable loss functions unless outliers demand robust losses.
Reality: Median requires ordinal or numeric data with a meaningful sort order. For nominal categories, use Mode. For multi-class predictions, report per-class metrics rather than a “median class.”
Knowledge Check
- Computation: Median of {5, 2, 8, 2, 9}? Answer: Sorted {2,2,5,8,9}; median = 5.
- Computation: Median of {1, 3, 4, 6}? Answer: (3+4)/2 = 3.5.
- True/False: The median is resistant to outliers. Answer: True.
- Multiple Choice: MAE loss is minimized by predicting the: (a) mean, (b) median, (c) mode, (d) max. Answer: (b).
- Short Answer: What percentile is the median? Answer: 50th percentile (p50).
- True/False: For perfectly symmetric data, mean equals median. Answer: True.
- Short Answer: Why use median imputation over mean imputation? Answer: Median is not pulled by extreme values in the column.
- Multiple Choice: p95 latency means: (a) 95% of requests are slower, (b) 5% of requests are slower, (c) median latency, (d) mean latency. Answer: (b).
- Computation: Data {10, 12, 11, 1000}: mean = 258.25. What is the median? Answer: 11.5.
- Short Answer: What loss connects median to regression? Answer: L1 loss / MAE (minimizes absolute deviations).
Key Takeaways
- The median is the middle value of sorted data—the 50th percentile.
- It is robust to outliers; the mean is not.
- Median minimizes absolute error; mean minimizes squared error.
- MAE regression targets conditional medians; MSE targets conditional means.
- Median imputation and RobustScaler use medians for outlier-resistant preprocessing.
- Production metrics should report percentiles (p50, p95, p99), not just averages.
- Next: Mode for the most frequent value, especially in categorical data.
Teaching strategy: Draw a right-skewed distribution. Mark mean (pulled right) and median (near the peak). Students remember the geometry long after forgetting formulas.
Hands-on idea: Compare SimpleImputer(strategy='mean') vs strategy='median' on a column with one extreme outlier. Show how each affects downstream model coefficients.
Discussion prompt: When evaluating a recommender system, should you optimize for mean engagement or median engagement? Who wins and who loses in each case?