I3 · Mathematics for AI

Mean, Median and Mode

These are the three measures of central tendency, meaning a typical value of a dataset. The mean is the sum divided by the count. The median is the middle value after sorting. The mode is the most frequent value.

10 min Intermediate

What you need: The earlier topics of this module.

Definition

These are the three measures of central tendency, meaning a typical value of a dataset. The mean is the sum divided by the count. The median is the middle value after sorting. The mode is the most frequent value.

In simple words

Five friends earn 20, 25, 30, 35 and 40 thousand rupees. The mean and median are both 30. Then a crorepati joins. The mean jumps massively, and now describes nobody in the group, while the median barely moves. That's why salary reports usually quote the median.

Why it matters

It's usually the first thing you check on new data. Filling in missing values needs a choice between mean, median and mode. A large gap between mean and median warns you about outliers or skew before they damage a model.

How it works

MeasureAffected by outliers?Use it for
MeanYes, stronglySymmetric numeric data, such as heights
MedianBarelySkewed data, such as income and prices
ModeNoCategories, such as the most common city

Finding the median

  1. Sort the values.
  2. With an odd count, take the middle one.
  3. With an even count, average the two middle ones.

The maths

Mean
The mean, read "x bar"
How many values
Weighted mean
The weight of value i, such as an exam's credit hours

Data: 2, 3, 3, 5, 7, 40.

Mean   = (2+3+3+5+7+40) / 6 = 60/6 = 10
Median = (3+5) / 2 = 4
Mode   = 3

The mean of 10 is higher than five of the six values, because of the outlier 40. The median of 4 describes this data better.

In code

centre.py
import statistics as st

data = [2, 3, 3, 5, 7, 40]
print("Mean:", st.mean(data))
print("Median:", st.median(data))
print("Mode:", st.mode(data))

without_outlier = data[:-1]
print("Mean without 40:", st.mean(without_outlier))
print("Median without 40:", st.median(without_outlier))

Output

Mean: 10
Median: 4.0
Mode: 3
Mean without 40: 4
Median without 40: 3

Common mistakes

  • Reporting only the mean for skewed data. Check the median too.
  • Forgetting to sort before finding the median.
  • Taking the mean of categories coded as numbers. The average of city codes means nothing.

Check yourself

Question 1

1. What is the median of 7, 1, 3, 9, 5?

Question 2

2. Which is best for typical house prices in a city with some mansions?

Summary

  • Mean: the average. Median: the middle. Mode: the most common.
  • Outliers pull the mean, but barely move the median.
  • A big gap between mean and median signals skew.

Finished reading?