Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Mean, variance and standard deviation

After this lesson: I can summarise data with a centre and a spread.

55 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Why the mean is not enough

A standard deviation is a number that says how far values usually sit from their mean. You need it because two data sets can share a mean and still behave very differently.

Two bike stations can both hire 240 bikes a day on average. One may hire close to 240 every day. The other may swing between 100 and 380.

By the end of this lesson

Note

I can summarise data with a centre and a spread.

Key words

WordWhat it meansExample
Meanthe total of the values divided by how many there are
Spreadhow far apart the values are
Quartilea value that cuts ordered data into quartersthe first quartile
Variancethe mean of the squared gaps from the mean
Standard deviationthe square root of the variance, in the same unit as the data
Samplea part of the data used to describe the whole

The cells students fill in are left blank in this preview.

Two data sets can share a mean

The mean tells you where the middle of the data is. It says nothing about how far the values stray from it. That second fact is the spread.

The simplest measure of spread is the range: the highest value minus the lowest.

Worked example: same mean, different range

StationHires on five daysMeanHighest minus lowest
A238, 240, 242, 240, 2401200 / 5 = 240242 - 238 = 4
B100, 380, 240, 150, 3301200 / 5 = 240380 - 100 = 280

The cells students fill in are left blank in this preview.

Quartiles ignore the extreme days

The range uses only two days, the highest and the lowest. One odd day can change it a lot.

A quartile cuts ordered data into quarters. The first quartile has a quarter of the values below it. The third has three quarters below it. The gap between them is the interquartile range. It is the spread of the middle half of the days.

Find the range and quartiles

Command
docker exec jupyter python3 -c "import pandas as pd; h = pd.read_csv('hires.csv'); print(h['hires'].min(), h['hires'].max(), h['hires'].quantile(0.25), h['hires'].quantile(0.75))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Variance is the mean of the squared gaps

A better spread uses every value. For each value, find its gap from the mean. Square each gap, so that a gap below the mean does not cancel a gap above it. Then take the mean of the squares. That is the variance.

The variance is in squared units, such as bikes squared. Its square root, the standard deviation, is back in bikes.

Worked example: variance by hand

DayHiresGap from mean 240Gap squared
1180-603600
2210-30900
324000
426020400
5310704900
Total120009800
ResultMean 240Variance 9800 / 5 = 1960Standard deviation 44.3

The cells students fill in are left blank in this preview.

Work out the variance in Python

Code: python
# Five days of hires at one station.
days = [180, 210, 240, 260, 310]

total = 0
for one in days:
    total = total + one
centre = total / len(days)

squares = 0
for one in days:
    gap = one - centre
    squares = squares + gap * gap
print("mean:", centre)
print("variance:", squares / len(days))
print("standard deviation:", round((squares / len(days)) ** 0.5, 1))
# Change 310 to 500 and run again.
# Which of the three numbers grows the most?

A sample divides by n minus 1

When your data is every value there is, divide the squared gaps by how many values you have, n. That gives the population variance.

When your data is only a sample of a bigger group, divide by n - 1 instead. A sample sits closer to its own mean than the whole group sits to the true mean, and the smaller divisor makes up for it.

Worked example: two divisors

Divide the 9800 byVarianceStandard deviation
n = 5, the whole group196044.3
n - 1 = 4, a sample245049.5

The cells students fill in are left blank in this preview.

Compare pandas and NumPy

Command
docker exec jupyter python3 -c "import numpy as np, pandas as pd; v = pd.read_csv('hires.csv')['hires'].head(5); print(round(v.std(), 1), round(np.std(v.to_numpy()), 1))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think every library gives the same standard deviation. It does not. pandas divides by n - 1 and NumPy divides by n. Use the ddof setting to choose, and always know which one you are reading.

Going further

In your own notebook, find the standard deviation of temp_c, then of rain_mm. Which one varies more in its own unit?

Then find a pair of columns where the standard deviations are close but the units differ. Say why comparing them directly would mislead a reader.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.