Menu
CoddyTech

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:

Five espresso shots as dots around an orange mean line at 26 seconds. A gray stick joins each dot to the line, labelled with its deviation: -2, +1, -1, +4 and -2

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.28

Its 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 icon

Challenge

Easy

Quality 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:

  1. the variance: the mean of the squared deviations from the mean of a,
  2. 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: True

Try 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))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler