AI Hints
Statistics & Math for ML · Lesson 8 of 138

The Median

The middle value.

⏱ 2 min read▶ Video at 7:56🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

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.

The Median
From the video: The Median
  • 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.

keyvalue
Medianthe middle of the sorted data
Even countaverage the two middles
np.mediandoes the sorting
Robustignores 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