Variance and Std
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 3 of 64.
.std() is the typical distance from the mean. Here it is built by hand, for five espresso shots:
Each gap to the mean is a deviation. They cancel to 0, so square them, then average: the variance.
shots = np.array([24, 27, 25, 30, 24])
deviations = shots - shots.mean()
variance = (deviations ** 2).mean()
print(deviations)
print(variance, np.round(np.sqrt(variance), 2))This prints:
[-2. 1. -1. 4. -2.]
5.2 2.28Its square root, the standard deviation, turns seconds squared back into seconds: 2.28. np.var(a) and np.std(a) do each in one call.
A feature with a std of 0 never changes, so it tells a model nothing.
Challenge
EasyQuality control checks how evenly a machine fills juice bottles. The starter reads six volumes in ml and prints what spread_by_hand(a) returns, next to np.std. Complete that function so it returns:
- the variance: the mean of the squared deviations from the mean of
a, - the standard deviation, its square root.
For the volumes 1004, 996, 1011, 993, 1002 and 998:
Variance: 34.56 ml squared
Std: 5.88 ml
Same as np.std: TrueTry it yourself
import numpy as np
values = []
for i in range(6):
values.append(int(input()))
volumes = np.array(values)
# Returns the variance and the standard deviation of the array a
def spread_by_hand(a):
# Write your code here, then return the two values in place of 0, 0
return 0, 0
# Don't change the lines below
variance, std = spread_by_hand(volumes)
print(f"Variance: {np.round(variance, 2)} ml squared")
print(f"Std: {np.round(std, 2)} ml")
print("Same as np.std:", np.round(std, 2) == np.round(np.std(volumes), 2))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler