Mean, Median and Standard Deviation with NumPy
Summarize a list of scores with three numbers: where the middle is and how spread out the values are.
- np.mean and np.median
- np.std and what spread means
- When the median is better than the mean
mean84.0median85.0 (the middle value)std7.13 (typical distance from 84)Center and spread
The problem
A list of 100 scores is hard to read. You want two quick answers: what is a typical score, and how spread out are they? The mean, median and standard deviation answer exactly those questions.
Think of it like this
Imagine students standing on a number line by score. The mean is the balance point of the line, the median is the student standing in the middle of the queue, and the standard deviation tells you how far students usually stand from the balance point.
np.mean(scores)⚖️ The balance point: 84.0np.median(scores)🧍 The student in the middle: 85.0np.std(scores)↔️ Typical distance from the center: 7.13round(..., 2)✂️ Keep two decimalsThe mean is the sum divided by the count, the median is the middle value after sorting, and the standard deviation measures how far values typically lie from the mean.
Key points
np.mean(scores)adds the five scores and divides by 5: 84.0.np.median(scores)sorts them and takes the middle one: 85.0.np.std(scores)measures the spread;round(..., 2)shows 7.13.- One very low score would pull the mean down much more than the median.
When should you use it?
- Summarizing scores, prices, times and measurements.
- Spotting unusual values that lie far from the mean.
- Comparing two groups, such as two classes or two products.
When not to use it
- Data with extreme outliers, such as incomes: the median describes a typical value better than the mean.
- A sample from a larger population: use
np.std(scores, ddof=1)for the sample standard deviation.
Example code
import numpy as np
scores = np.array([72, 81, 85, 90, 92])
print("mean:", np.mean(scores))
print("median:", np.median(scores))
print("std:", round(np.std(scores), 2))
Result of running the code
mean: 84.0
median: 85.0
std: 7.13
Code explanation
NumPy calculates each statistic over the entire array by default. The standard deviation describes typical distance from the mean.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input shape and dtype.
- Try negative, floating-point, and missing values.
- Apply the operation to larger multidimensional arrays and measure performance.
Common mistakes
Using the population formula for a sample
np.std divides by n by default; a sample needs n - 1.
np.std(sample)
np.std(sample, ddof=1)
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Z-Score Standardization Center a feature at 0 and scale it to a standard deviation of 1. Easy
- Median of a List Find the middle value of a list after sorting. Easy
- Array Average Compute the average of a list of numbers. Easy