Median — The Middle Ground
When outliers distort the average, the median gives you the true centre of your data.
The median is the middle value when data is sorted. If two values share the middle, average them together.
EXERCISE
1What Is the Median?
The median is the value that sits right in the middle — half the data is above it, half is below.
Imagine lining up 13 people by height. The median height is the 7th person — exactly in the middle. It does not matter if the tallest person is 6 feet or 60 feet — the middle person stays the same.
The rule is simple: sort the values, then pick the middle one.
speed = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
# Step 1: Sort
sorted_speed = sorted(speed)
print(sorted_speed)
# Output: [77, 78, 85, 86, 86, 86, 87, 87, 88, 94, 99, 103, 111]
# Step 2: Pick the middle (13 values → index 6)
print(sorted_speed[6])
# Output: 87
Using NumPy:
import numpy
speed = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
x = numpy.median(speed)
print(x)
# Output: 87.0
Pro Tip: The median is unmoved by outliers. Whether your highest value is 111 or 11,000, the middle value stays the same. That is why median household income is a better measure than mean household income.
EXERCISE
2Even vs Odd — How the Median Handles Both Cases
When there is no single middle value, the median splits the difference — and that small detail trips up a lot of beginners.
With an odd number of values, the median is obvious — it is the one in the centre. With an even number, there are two middle values. The median is their average.
Odd count (13 values):
import numpy
speed = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
# Sorted: [77, 78, 85, 86, 86, 86, >>87<<, 87, 88, 94, 99, 103, 111]
# 13 values → middle is position 7 → value is 87
print(numpy.median(speed))
# Output: 87.0
Even count (12 values):
import numpy
speed = [99, 86, 87, 88, 86, 103, 87, 94, 78, 77, 85, 86]
# Sorted: [77, 78, 85, 86, 86, >>86, 87<<, 87, 88, 94, 99, 103]
# 12 values → two middle values: 86 and 87
# Median = (86 + 87) / 2 = 86.5
print(numpy.median(speed))
# Output: 86.5
Manual calculation for even counts:
data = [10, 20, 30, 40]
# Sorted: [10, 20, 30, 40]
# Two middle values: 20 and 30
median = (20 + 30) / 2
print(median)
# Output: 25.0
Pro Tip: NumPy handles the odd/even logic automatically. But understanding it matters because when you are debugging ML results and the median seems "off", it is usually because the data has an even count and the median is a value that does not actually exist in the data set.
EXERCISE
3Mean vs Median — When to Reach for Each
Knowing when to use mean versus median is the difference between a helpful summary and an accidental lie.
Mean and median are like two different weather forecasts. The mean says "the average this week was 25°C" — but that hides the fact that Monday was 5°C and Sunday was 45°C. The median says "the middle day was 25°C" — and that is usually more representative.
Side-by-side comparison:
import numpy
# Symmetric data — mean ≈ median
symmetric = [10, 12, 14, 15, 16, 18, 20]
print(f"Mean: {numpy.mean(symmetric)}, Median: {numpy.median(symmetric)}")
# Output: Mean: 15.0, Median: 15.0
# Skewed data — mean ≠ median
skewed = [10, 12, 14, 15, 16, 18, 200]
print(f"Mean: {round(numpy.mean(skewed), 1)}, Median: {numpy.median(skewed)}")
# Output: Mean: 40.7, Median: 15.0
# The median barely moved. The mean jumped from 15 → 40.7!
| Scenario | Winner | Why |
|---|---|---|
| Exam scores (no outliers) | Mean | Uses all data fairly |
| House prices in a city | Median | A few mansions skew the mean |
| Sensor readings | Mean | Usually normally distributed |
| Income data | Median | Billionaires distort the mean |
Pro Tip: In ML, you will often see both reported together. If the mean and median are close, your data is roughly symmetric — great news. If they are far apart, your data is skewed and you need to decide: transform it, use a robust metric, or accept the skew in your model.
