AI Hints
Statistics & Math for ML · Lesson 14 of 138

Variance

Average squared distance.

⏱ 2 min read▶ Video at 14:44🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

To measure spread using every value, ask, how far is each value from the mean? Then average those distances. There's just one catch, which we'll solve with squaring.

Variance
From the video: Variance
  • How far from the mean?
  • For every value
  • Then average

Step by step

First, the deviations, each score minus the mean. Some are negative, and if we just averaged them, they'd cancel out to zero. So we square them, which makes them all positive. The variance is the mean of the squared deviations. Run it. NP dot var gives the same answer, three hundred and twenty four.

variance.pypython
import numpy as np

scores = np.array([55, 72, 90, 40, 81])
dev = scores - scores.mean()
print("deviations:", dev.round(1))
print("sum:", dev.sum().round(1))
print("variance:", (dev ** 2).mean().round(2))
print("np.var:  ", np.var(scores).round(2))
Output
deviations: [-12.6   4.4  22.4 -27.6  13.4]
sum: 0.0
variance: 324.24
np.var:   324.24

Samples divide by N minus one

There's a small twist. When you compute the variance of a sample, to estimate a population, you divide by N minus one instead of N. It corrects a slight underestimate. In NumPy, that's NP dot var with DDOF equals one. Pandas uses N minus one by default.

keyvalue
Populationdivide by n
Sampledivide by n − 1
NumPynp.var(x, ddof=1)
pandasn − 1 by default

Summary

The variance is the average squared distance from the mean. For a sample, divide by N minus one. Next, the standard deviation, which puts it back in normal units.

Key takeaways
  • Deviations from the mean
  • Square, then average
  • ddof=1 for samples
Next up: Standard Deviation — spread in real units