Variance
- Machine Learning course
- Beginner
- Lesson 28/112
- 5 min
Variance is the square of standard deviation and measures data spread.
Variance
Variance is the mean of the squared differences from the mean; it equals the square of the standard deviation:
x = np.array([1, 2, 3, 4, 5])
print(x.var()) # 2.0
print(((x - x.mean()) ** 2).mean()) # 2.0, by hand
print(np.sqrt(x.var()), x.std()) # 1.414... 1.414...Its unit is the square of the data's unit (cm²), so the standard deviation is usually preferred for interpretation.
Why does variance matter in ML?
A feature with near-zero variance (almost the same in every example) carries no information for the model. Methods like PCA rotate the data towards the directions of largest variance.
X = np.array([[1, 5.0], [2, 5.0], [3, 5.1]])
print(X.var(axis=0)) # [0.667 0.002] second column almost constantThe "bias-variance trade-off" is a different concept: it describes how much the model changes across different training data; you'll see it later.
Practice Find the constant column
Print the column variances of X. Then find the indices of columns whose variance is below 0.01 with np.where and print them.
Starter code
import numpy as np
X = np.array([[1, 7, 0], [3, 7, 4], [5, 7, 8]])
# buraya yazSample solution
import numpy as np
X = np.array([[1, 7, 0], [3, 7, 4], [5, 7, 8]])
v = X.var(axis=0)
print(v)
print(np.where(v < 0.01)[0])Expected output
[ 2.66666667 0. 10.66666667] [1]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the variance of data whose standard deviation is 3?
- 1.73
- 6
- 9 Correct answer
- 3
Explanation: Variance = standard deviation² = 3² = 9.
2 Compute the variance of each column.
v = X.____(axis=0)
v = X.var(axis=0)
Explanation: var(axis=0) gives the variance per column.
3 Match the relationships.
| Term | Meaning |
|---|---|
var() |
Mean of squared differences |
std() |
Square root of variance |
Zero variance |
Constant feature |
PCA |
Direction of largest variance |