Describing data
Summarise a set of measurements with the mean, median, range and standard deviation, and learn what each one hides.
Lesson 1 of 6 · 30 min · Self-paced · Free
Why summarise?
Suppose you are testing a robot gripper and you time how long it takes to close, nine times in a row. A teammate asks: "How fast is it?" You could read out all nine numbers, but after nine hundred trials that stops working. Engineers need a few numbers that capture where the data sits and how spread out it is. These are called summary statistics, and choosing the right ones is the first step of almost every analysis.
Here is the data we will use throughout this lesson. It is made up for illustration:
Gripper closing time (ms), 9 trials:
212, 208, 215, 210, 209, 214, 211, 213, 260Eight of the trials are close together. One, 260 ms, is much slower. Keep an eye on it.
The mean
The mean (the everyday "average") is the sum of the values divided by how many there are:
mean = (x₁ + x₂ + … + xₙ) / n
= 1952 / 9
≈ 216.9 msThe mean uses every value, which is its strength and its weakness. Notice that 216.9 ms is larger than eight of the nine trials. The single slow trial has dragged it upwards.
The median
The median is the middle value once the data is sorted:
208, 209, 210, 211, 212, 213, 214, 215, 260
^^^With nine values the middle one is the 5th, so the median is 212 ms. With an even number of values, the median is the mean of the two middle ones. The median barely cares how extreme the largest value is: if the slow trial had been 2600 ms, the median would still be 212 ms. We say the median is robust to outliers, while the mean is not.
Neither is "better". If you care about the total time for a thousand grasps, the mean is what matters, because slow trials really do add up. If you want a typical trial, the median often describes it more honestly.
The range
The range is the largest value minus the smallest: 260 − 208 = 52 ms. It is easy to compute and easy to understand, but it depends only on the two most extreme values, so one odd trial changes it completely. It also tends to grow as you collect more data, because you get more chances to see an extreme.
The standard deviation
We want a measure of spread that uses every value. The idea: find how far each value is from the mean (its deviation), square those distances so negatives do not cancel positives, average them, and take the square root to get back to the original units.
Let us do it by hand for a tiny set, 4, 6, 7, 8, 10, whose mean is 7:
value deviation deviation²
4 −3 9
6 −1 1
7 0 0
8 1 1
10 3 9
sum = 20Now there is a choice to make, and it matters.
- If these five values are the entire population you care about, divide by n = 5. The variance is 20 / 5 = 4 and the population standard deviation is √4 = 2.
- If they are a sample from a larger process (for example, five trials of a gripper that will close thousands of times), divide by n − 1 = 4. The variance is 20 / 4 = 5 and the sample standard deviation is √5 ≈ 2.236.
Why n − 1? The deviations are measured from the sample's own mean, which is by construction as close as possible to these particular values. That makes the squared deviations slightly too small on average compared with deviations from the true, unknown mean. Dividing by n − 1 instead of n corrects for this. In engineering you almost always have a sample, so the n − 1 version is the usual default. It is written s.
Python's statistics module has both:
import statistics
values = [4, 6, 7, 8, 10]
print(statistics.pstdev(values)) # population: divide by n
print(statistics.stdev(values)) # sample: divide by n - 1Output:
2.0
2.23606797749979A rough guide: for data that bunches around the middle, most values lie within about one standard deviation of the mean, and very few lie more than three away.
Summarising the gripper data in Python
import statistics
# Made-up data for illustration: time (ms) for a gripper to close, 9 trials
times = [212, 208, 215, 210, 209, 214, 211, 213, 260]
print("n =", len(times))
print("mean =", round(statistics.mean(times), 1))
print("median =", statistics.median(times))
print("range =", max(times) - min(times))
print("stdev =", round(statistics.stdev(times), 1))Output:
n = 9
mean = 216.9
median = 212
range = 52
stdev = 16.3Now compare the same statistics with and without the slow trial:
import statistics
times = [212, 208, 215, 210, 209, 214, 211, 213, 260]
clean = times[:-1] # the same trials without the 260 ms one
for label, data in [("all 9", times), ("first 8", clean)]:
print(f"{label:8s} mean {statistics.mean(data):6.1f} "
f"median {statistics.median(data):6.1f} "
f"stdev {statistics.stdev(data):5.1f}")Output:
all 9 mean 216.9 median 212.0 stdev 16.3
first 8 mean 211.5 median 211.5 stdev 2.4The median moved by half a millisecond. The mean moved by more than 5 ms, and the standard deviation shrank from 16.3 ms to 2.4 ms. One trial out of nine was responsible for almost all of the apparent spread. That is a clue worth following: was the gripper snagging on something? Was a cable caught? The summary statistics have not answered the question, but they have told you where to look.
What to report
A good description of a dataset usually includes:
- n, the number of values. A mean of 5 values and a mean of 500 deserve different levels of trust.
- A centre: the mean, the median, or both when they disagree.
- A spread: the standard deviation (say whether it is sample or population) and often the minimum and maximum.
- Units, every time.
Better still, look at the data itself before trusting any summary. Very different datasets can share the same mean and standard deviation. The next lessons show how to picture data and how to say how certain a mean is.
Project
Measure your reaction time with a ruler
You need a 30 cm ruler, a partner, a pencil and paper, and Python.
- Your partner holds the ruler vertically with the 0 mark between your open finger and thumb. Without warning, they let go and you catch it. Record the mark where you caught it, in centimetres.
- Do 20 trials and write every result down, including any you fumble.
- Convert each distance to a reaction time using the falling-object formula t = √(2d / g), with d in metres and g = 9.81 m/s². For example, 15 cm gives t = √(2 × 0.15 / 9.81) ≈ 0.175 s.
- In Python, compute n, the mean, median, range and sample standard deviation of your times.
- Repeat the summary for your first 10 and last 10 trials. Did you get faster with practice?
Deliverables
- A table of all 20 distances and reaction times
- A Python script that prints n, mean, median, range and sample standard deviation
- Two or three sentences comparing the mean and median, and the first and last 10 trials
Knowledge check
Check your understanding
Go further
- Python documentation — statistics module ↗
Official reference for mean, median, stdev, pstdev and the other functions used in this module.
- Standard deviation — Wikipedia ↗
More examples, plus the difference between population and sample standard deviation.
Lesson materials · free to download
Sign in to track your progress. Reading is free without an account.