Definition
Variance is the average squared distance of values from their mean. Standard deviation is its square root, so it's in the same units as the data. Both measure how spread out the data is.
In simple words
Two classes both average 60 marks. In one, everybody scored between 58 and 62. In the other, marks run from 10 to 100. The averages match, but the classes are very different. Standard deviation is the number that captures this.
Why it matters
A column with zero variance tells a model nothing, so you can drop it. Feature scaling divides by standard deviation. PCA looks for directions of maximum variance. It's also how you judge whether a result is noise or a real effect.
How it works
- Find the mean.
- Subtract the mean from each value. These are the deviations.
- Square each one, so negatives don't cancel positives.
- Average the squares. That's the variance.
- Take the square root. That's the standard deviation.
Why divide by n−1?
For a whole population, divide by n. For a sample, divide by n−1. A sample's mean sits closer to its own values than the true mean does, so the plain average underestimates the spread. Using n−1, called Bessel's correction, fixes that.
The maths
- The sample standard deviation
- The sample mean
Sample: 2, 4, 6, 8. The mean is 5.
Deviations: −3, −1, 1, 3 Squares: 9, 1, 1, 9 sum = 20 s² = 20 / (4−1) = 6.67 s = √6.67 ≈ 2.58
In code
import numpy as np
import statistics as st
data = [2, 4, 6, 8]
print("statistics.stdev (n-1):", round(st.stdev(data), 3))
print("numpy std (n):", round(np.std(data), 3))
print("numpy std with ddof=1 (n-1):", round(np.std(data, ddof=1), 3))
calm = [58, 59, 60, 61, 62]
wild = [10, 30, 60, 90, 110]
print("Same mean?", st.mean(calm), st.mean(wild))
print("Spread:", round(st.stdev(calm), 1), round(st.stdev(wild), 1))Output
statistics.stdev (n-1): 2.582 numpy std (n): 2.236 numpy std with ddof=1 (n-1): 2.582 Same mean? 60 60 Spread: 1.6 41.2
Common mistakes
- Using population and sample formulas interchangeably on small data, where the difference is noticeable.
- Comparing the standard deviations of variables with different units or scales without standardising first.
Check yourself
Summary
- Variance: the average squared distance from the mean.
- Standard deviation: its square root, in the original units.
- For samples, divide by n−1.
Finished reading?