Mean — The Average Truth
The mean is the most famous statistic on earth — but it is easily fooled by extreme values.
The mean is the sum of all values divided by the count. Use NumPy's mean() to calculate it in one line.
EXERCISE
1What Is the Mean?
The mean is the most used statistic in the world — add everything up and divide by how many you have.
Imagine splitting a restaurant bill perfectly equally among friends. If four friends spend $20, $30, $40, and $50 — the mean is $35 each. Everyone pays the same "average" amount, even though nobody actually spent $35.
The formula:
Mean = Sum of all values ÷ Number of values
# Manual calculation
speed = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
mean = sum(speed) / len(speed)
print(round(mean, 2))
# Output: 89.77
Using NumPy (the ML way):
import numpy
speed = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
x = numpy.mean(speed)
print(round(x, 2))
# Output: 89.77
Pro Tip: The mean is powerful because it uses every value in the data set. But this is also its weakness — a single extreme value (outlier) can drag the mean far from where most of your data actually lives.
EXERCISE
2How the Mean Is Calculated Step by Step
Understanding the mechanics behind the mean helps you spot when it is lying to you — and outliers are its favourite trick.
Think of the mean as a balance point on a seesaw. If you place weights (your data values) along a beam, the mean is where the beam perfectly balances. One heavy weight far from the centre can tip the whole thing.
Step-by-step calculation:
speed = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
# Step 1: Sum all values
total = sum(speed)
print(f"Sum: {total}")
# Output: Sum: 1167
# Step 2: Count the values
count = len(speed)
print(f"Count: {count}")
# Output: Count: 13
# Step 3: Divide
mean = total / count
print(f"Mean: {round(mean, 2)}")
# Output: Mean: 89.77
Watch what an outlier does:
import numpy
normal_data = [85, 86, 87, 88, 89, 90, 91]
print(f"Normal mean: {numpy.mean(normal_data)}")
# Output: Normal mean: 88.0
with_outlier = [85, 86, 87, 88, 89, 90, 500]
print(f"Outlier mean: {round(numpy.mean(with_outlier), 2)}")
# Output: Outlier mean: 146.43
# One value of 500 dragged the mean from 88 to 146!
Pro Tip: When your data has outliers, the mean becomes misleading. This is exactly why ML practitioners check for outliers before calculating statistics. A salary data set with one billionaire will make the "average salary" look fantastic — but nobody else actually earns that much.
EXERCISE
3Mean in Practice — When to Use It and When to Avoid It
The mean is your go-to summary for symmetric data — but for skewed data, it will confidently give you the wrong answer.
Using the mean on skewed data is like reporting the average number of legs in a room of people and a snake. Technically correct, practically useless.
When the mean works well:
import numpy
# Symmetric data — mean is representative
exam_scores = [72, 75, 78, 80, 82, 85, 88]
print(f"Mean: {numpy.mean(exam_scores)}")
# Output: Mean: 80.0
# The mean (80) IS where most scores cluster — good summary!
When the mean misleads:
import numpy
# Skewed data — most people earn far less than the mean
salaries = [30000, 32000, 35000, 38000, 40000, 950000]
print(f"Mean salary: {round(numpy.mean(salaries), 2)}")
# Output: Mean salary: 187500.0
print(f"Median salary: {numpy.median(salaries)}")
# Output: Median salary: 36500.0
# The median (36,500) better represents "typical" salary
Quick decision guide:
| Data Shape | Use Mean? | Better Alternative |
|---|---|---|
| Symmetric (bell-shaped) | ✅ Yes | — |
| Skewed (long tail) | ❌ Misleading | Median |
| Has outliers | ❌ Distorted | Median or trimmed mean |
| Categorical | ❌ Meaningless | Mode |
Pro Tip: In ML, the mean is used everywhere — from calculating errors to normalising features. But the best data scientists always check the distribution shape first. If you blindly use the mean on skewed data, your model will optimise for the wrong target.
