Variance is standard deviation's secret twin — square the deviation and you get variance, take the square root and you are back.
Save
Complete lesson & earn 250 PX
EXERCISE
1Variance is standard deviation before it takes the square root — it tells you the same story, just in squared units.
Save
EXERCISE
2Variance is just four mechanical steps — find the mean, subtract, square, and average. No creativity required.
Save
EXERCISE
3Both measure spread, but one speaks your language and the other speaks math's language.
Save
If standard deviation is the height of a fence, variance is the area of the fence board — related, but in different units. You can always convert between them: square the std dev to get variance, or take the square root of variance to get std dev.
The relationship:
Variance = σ² (standard deviation squared)
Standard Deviation = √Variance
import numpy
speed = [32, 111, 138, 28, 59, 77, 97]
variance = numpy.var(speed)
std_dev = numpy.std(speed)
print(f"Variance: {round(variance, 2)}")
# Output: Variance: 1432.24
print(f"Std Dev: {round(std_dev, 2)}")
# Output: Std Dev: 37.85
print(f"√Variance: {round(variance ** 0.5, 2)}")
# Output: √Variance: 37.85
print(f"Std² : {round(std_dev ** 2, 2)}")
# Output: Std² : 1432.24
> 💡 Key Insight: Variance is mathematically cleaner than standard deviation (no square root), which is why it appears more often in formulas and proofs. But standard deviation is easier to interpret because it is in the same units as your data. In ML, you will see both — know they are two sides of the same coin.
Think of variance as an error report. Each value has a "distance" from the mean. You square those distances (so negatives do not cancel positives), then average them. That average squared distance is variance.
Full walkthrough:
speed = [32, 111, 138, 28, 59, 77, 97]
# Step 1: Find the mean
mean = sum(speed) / len(speed)
print(f"Mean: {mean}")
# Output: Mean: 77.42857142857143
# Step 2: Difference from mean
diffs = [x - mean for x in speed]
print([round(d, 1) for d in diffs])
# Output: [-45.4, 33.6, 60.6, -49.4, -18.4, -0.4, 19.6]
# Step 3: Square each difference
squared = [d ** 2 for d in diffs]
print([round(s, 2) for s in squared])
# Output: [2061.16, 1128.96, 3672.36, 2440.36, 338.56, 0.16, 384.16]
# Step 4: Average of squared differences
variance = sum(squared) / len(squared)
print(f"Variance: {round(variance, 2)}")
# Output: Variance: 1432.25
Verify with NumPy:
import numpy
speed = [32, 111, 138, 28, 59, 77, 97]
print(round(numpy.var(speed), 2))
# Output: 1432.24
> 💡 Key Insight: You might notice a tiny difference between manual and NumPy calculations — this is due to floating-point rounding in computers, not an error. In ML, these tiny differences are irrelevant. What matters is the magnitude: a variance of 1,432 tells you the data is very spread out.
Variance is measured in squared units. If your data is speed in km/h, variance is in km²/h². That is hard to interpret! Standard deviation brings it back to km/h — which your brain can actually understand.
When to use which:
| Metric | Units | Best For |
|---|---|---|
| Variance (σ²) | Squared (km²/h²) | Math formulas, comparing distributions |
| Std Dev (σ) | Original (km/h) | Interpreting spread, explaining to humans |
import numpy
# Temperature in °C
temps = [22.1, 23.4, 21.8, 24.0, 22.5]
var = numpy.var(temps)
std = numpy.std(temps)
print(f"Variance: {round(var, 2)} °C²")
# Output: Variance: 0.57 °C²
print(f"Std Dev: {round(std, 2)} °C")
# Output: Std Dev: 0.76 °C
# "Temperatures vary by about 0.76°C" makes sense
# "Temperatures vary by 0.57 degrees-squared" does not
Practical ML example — comparing feature spreads:
import numpy
heights = [165, 170, 175, 180, 185]
weights = [55, 65, 70, 80, 95]
print(f"Height variance: {numpy.var(heights)}")
# Output: Height variance: 50.0
print(f"Weight variance: {numpy.var(weights)}")
# Output: Weight variance: 170.0
# Weight has MORE spread — this matters when scaling features
> 💡 Key Insight: In ML, variance appears in the formula for many algorithms — (Principal Component Analysis) finds the direction of maximum variance, and is one of the most important concepts in model evaluation. You will meet variance again and again throughout your ML journey.
Variance is the average of squared differences from the mean. Standard deviation is just its square root.