AI Hints
Home›Statistics & Math for ML Tutorial›Choosing an Average
Statistics & Math for ML · Lesson 10 of 138

Mean or Median?

Outliers pull the mean.

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

A small company has seven workers, and a boss. The boss says the average salary is over a hundred thousand. The workers say they earn about forty. Who's lying? Pause. Maybe nobody.

Mean or Median?
From the video: Mean or Median?
  • Seven workers and a boss
  • Average over 100k?
  • Workers earn about 40k

One outlier

Here are the salaries, in thousands. The last one is the boss. Run it. The mean is about a hundred and forty five, but the median is thirty nine. The boss's huge salary drags the mean up. The median tells you what a typical worker earns.

salaries.pypython
import numpy as np

salaries = np.array([30, 32, 35, 38, 40, 41, 45, 900])

print("mean:  ", salaries.mean().round(1))
print("median:", np.median(salaries))
Output
mean:   145.1
median: 39.0

Seeing the gap

Real incomes look like this. Most people earn a moderate amount, and a few earn a lot, which makes a long tail on the right. We draw a histogram, then a line for the mean, and one for the median. Run it. The mean sits to the right of the median, pulled by the tail.

income.pypython
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)
income = rng.lognormal(10.5, 0.6, 5000) / 1000

plt.hist(income, bins=60)
plt.axvline(income.mean(), color="#22d3ee", label="mean")
plt.axvline(np.median(income), color="#fbbf24", label="median")
plt.title("Incomes (thousands)"); plt.legend()
plt.savefig("income.png")

Summary

Outliers and long tails pull the mean, but not the median. For incomes and prices, report the median. Next, averages where some values count more.

Key takeaways
  • Outliers pull the mean
  • Median for skewed data
  • Check both
Next up: The Weighted Mean — some count more