Standard Deviation — How Spread Out Are We?
Two data sets can have the same average but feel completely different — standard deviation explains why.
Standard deviation measures how far values typically sit from the mean. Low means clustered, high means scattered.
EXERCISE
1What Is Standard Deviation?
Two classes can have the same average grade — but one class might be full of C students while the other has a mix of As and Fs.
Imagine two pizza delivery services. Both have an average delivery time of 30 minutes. But Service A always delivers between 28–32 minutes, while Service B delivers anywhere from 10–50 minutes. Same average, very different reliability. Standard deviation measures that reliability.
Low vs high standard deviation:
import numpy
# Service A: consistent (low spread)
service_a = [86, 87, 88, 86, 87, 85, 86]
print(f"Mean: {numpy.mean(service_a)}, Std: {round(numpy.std(service_a), 2)}")
# Output: Mean: 86.43, Std: 0.9
# Service B: inconsistent (high spread)
service_b = [32, 111, 138, 28, 59, 77, 97]
print(f"Mean: {numpy.mean(service_b)}, Std: {round(numpy.std(service_b), 2)}")
# Output: Mean: 77.43, Std: 37.85
What the number means:
- Std of 0.9 → most values are within 0.9 of the mean (very tight)
- Std of 37.85 → values are scattered up to ~38 units from the mean (very wide)
Pro Tip: Standard deviation is represented by the Greek letter σ (sigma). When someone says data is "within 2 sigma" they mean within 2 standard deviations of the mean — which covers about 95% of normally distributed data.
EXERCISE
2Calculating Standard Deviation Step by Step
Behind the scenes, standard deviation is just a fancy average of how far each value sits from the mean.
Think of it like measuring how far each student sits from the teacher''s desk (the mean). Some sit close, some sit far. Standard deviation is the average distance — but with a mathematical twist.
The steps:
- Find the mean
- Subtract the mean from each value (get "distances")
- Square each distance (to remove negatives)
- Average the squared distances → this is the variance
- Take the square root → this is the standard deviation
import numpy
speed = [86, 87, 88, 86, 87, 85, 86]
# Step 1: Mean
mean = numpy.mean(speed)
print(f"Mean: {round(mean, 2)}")
# Output: Mean: 86.43
# Step 2-3: Squared differences
diffs = [(x - mean) ** 2 for x in speed]
print([round(d, 2) for d in diffs])
# Output: [0.18, 0.33, 2.47, 0.18, 0.33, 2.04, 0.18]
# Step 4: Average of squared diffs (variance)
variance = sum(diffs) / len(diffs)
print(f"Variance: {round(variance, 2)}")
# Output: Variance: 0.82
# Step 5: Square root (std dev)
std = variance ** 0.5
print(f"Std Dev: {round(std, 2)}")
# Output: Std Dev: 0.9
Or just use NumPy:
import numpy
print(numpy.std([86, 87, 88, 86, 87, 85, 86]))
# Output: 0.9035079029052513
Pro Tip: We square the differences in step 3 because raw differences cancel out (some are positive, some negative, and they sum to zero). Squaring makes every distance positive. Then we take the square root at the end to get back to the original units.
EXERCISE
3Standard Deviation in Practice — The σ Rule
In normally distributed data, standard deviation gives you an exact map: 68% of values within 1σ, 95% within 2σ, 99.7% within 3σ.
The σ rule is like concentric fences around a bullseye. The first fence (1σ) catches 68% of all darts. The second fence (2σ) catches 95%. The third (3σ) catches 99.7%. Anything beyond 3σ is a true outlier.
The 68-95-99.7 rule:
import numpy
# Generate 100,000 normally distributed values
data = numpy.random.normal(100, 15, 100000)
mean = numpy.mean(data)
std = numpy.std(data)
# Count values within 1, 2, 3 standard deviations
within_1 = numpy.sum((data > mean - std) & (data < mean + std))
within_2 = numpy.sum((data > mean - 2*std) & (data < mean + 2*std))
within_3 = numpy.sum((data > mean - 3*std) & (data < mean + 3*std))
print(f"Within 1σ: {within_1/1000:.1f}%")
# Output: Within 1σ: ~68.3%
print(f"Within 2σ: {within_2/1000:.1f}%")
# Output: Within 2σ: ~95.4%
print(f"Within 3σ: {within_3/1000:.1f}%")
# Output: Within 3σ: ~99.7%
Practical use — detecting outliers:
import numpy
scores = [72, 75, 78, 80, 82, 85, 150]
mean = numpy.mean(scores)
std = numpy.std(scores)
outliers = [x for x in scores if abs(x - mean) > 2 * std]
print(f"Outliers (beyond 2σ): {outliers}")
# Output: Outliers (beyond 2σ): [150]
Pro Tip: In ML, standard deviation is used everywhere — from normalising features (StandardScaler divides by σ) to detecting anomalies (values beyond 3σ are flagged as suspicious). Understanding σ is not optional — it is fundamental to every pipeline.
