Statistics & Math for ML · Lesson 8 of 138
The Median
The middle value.
The median is the value in the middle. Sort the numbers, and pick the one in the centre. Half the data is below it, and half is above it.

- Sort the numbers
- Pick the middle one
- Half below, half above
Finding the middle
Sorted, our scores are forty, fifty five, seventy two, eighty one, and ninety. The middle one, the third of five, is seventy two. With an even count, there are two middle values, and the median is the mean of those two. Run it.
median.pypython
import numpy as np
scores = np.array([55, 72, 90, 40, 81])
print(np.sort(scores))
print("median:", np.median(scores))
even = np.array([40, 55, 72, 81])
print("median of four:", np.median(even))Output
[40 55 72 81 90] median: 72.0 median of four: 63.5
Mean or median?
The mean uses every value's size, while the median only cares about order. So if the top score were nine hundred instead of ninety, by mistake, the median wouldn't change at all. We say the median is robust. It isn't fooled by a few extreme values.
| key | value |
|---|---|
| Median | the middle of the sorted data |
| Even count | average the two middles |
| np.median | does the sorting |
| Robust | ignores extremes |
Summary
The median is the middle value of the sorted data, and extreme values don't move it. Next, the mode.
Key takeaways- Sort, take the middle
- Even: average two
- Robust to outliers
Next up: The Mode — the most common