ML Mean Median Mode

Mean, median, and mode are the three foundational measures of central tendency that describe the 'typical' value in a dataset.

Measures of Central Tendency

Before training any machine learning model, you need to understand the data you are feeding it. The mean, median, and mode are the three most common ways to describe where the center of a dataset lies. They answer the same basic question - what is a typical value here - but can give very different answers when your data contains outliers or skew.

Mean (the Average)

The mean is the sum of all values divided by the number of values. It uses every data point, which makes it sensitive to extreme values (outliers): a single unusually large or small number can pull the mean away from what most people would consider typical.

Example

import numpy as np
import statistics

scores = [72, 85, 90, 68, 74, 95, 100, 65]

print('Mean (numpy):', np.mean(scores))
print('Mean (statistics):', statistics.mean(scores))

# A single outlier can pull the mean noticeably
scores_with_outlier = scores + [10]
print('Mean with an outlier added:', np.mean(scores_with_outlier))

Median (the Middle Value)

The median is the middle value when the data is sorted from smallest to largest. If there is an even number of values, the median is the average of the two middle numbers. Because it only depends on the position of values rather than their size, the median is robust to outliers.

Example

import numpy as np
import statistics

salaries = [42000, 45000, 47000, 50000, 51000, 53000, 480000]

print('Mean salary:', np.mean(salaries))
print('Median salary (numpy):', np.median(salaries))
print('Median salary (statistics):', statistics.median(salaries))
# The median ignores the size of the outlier, only its position matters

Mode (the Most Frequent Value)

The mode is the value that appears most often in the dataset. Unlike mean and median, the mode can be used on non-numeric (categorical) data, and a dataset can have more than one mode (bimodal or multimodal) or no mode at all if every value is unique.

Example

import statistics

shoe_sizes = [8, 9, 9, 10, 10, 10, 11, 12]
ratings = [3, 5, 4, 5, 2, 5, 4]

print('Most common shoe size:', statistics.mode(shoe_sizes))
print('All modes in ratings:', statistics.multimode(ratings))

no_repeats = [1, 2, 3, 4, 5]
print('Modes with no repeats:', statistics.multimode(no_repeats))
MeasureBest used whenSensitive to outliers?
MeanData is roughly symmetric with no extreme outliersYes
MedianData is skewed or contains outliersNo
ModeData is categorical or has repeated valuesNo
  • Reporting only the mean can hide a skewed distribution - always check the median too
  • The mode is undefined (or every value is a mode) when all values are unique
  • For symmetric, outlier-free data, mean and median will be close together
  • Household income and house prices are classic examples where median beats mean
Note: Salaries are a classic example: a company with one very high-paid executive can have a mean salary far above what most employees actually earn. The median salary usually tells a more honest story.

Exercise: ML Mean Median Mode

Why might the median describe typical income better than the mean in a skewed dataset?