Menu
CoddyTech

Sample or Population?

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 4 of 64.

A beekeeper weighed the honey from 5 hives, in kg. NumPy and pandas disagree on the std:

values = [18, 22, 25, 19, 26]
print(np.round(np.std(values), 2))
print(np.round(pd.Series(values).std(), 2))

This prints:

3.16
3.54

Both add the same squared deviations, then divide:

The squared deviations of 5 hives, 16, 0, 9, 9 and 16, add up to 50. One arrow leads to np.std: 50 / 5 = 10, std 3.16, the population formula. The other to pandas: 50 / 4 = 12.5, std 3.54, the sample formula

Dividing by n suits a population, every value there is. A sample hugs its own mean, so its spread looks small; n - 1 makes up for it.

ddof=1 gives NumPy the sample formula: np.std(a, ddof=1) matches pandas. Model data is a sample: a std mismatch between tools is ddof, not a bug.

challenge icon

Challenge

Easy

A biologist got two different stds for a few bats' wingspans, in cm. The starter reads them into the array spans and the Series column; its check prints False.

  1. Bats are a sample: fix the NumPy line so both tools give the sample std.
  2. Below it, print how far NumPy's default np.std(spans) falls below column.std(), to 2 decimals.

For the wingspans 33, 27, 30, 35 and 29:

Sample std: 3.19 cm, tools agree: True
Dividing by n underestimated it by 0.34 cm

Try it yourself

import numpy as np
import pandas as pd

n = int(input())
values = []
for i in range(n):
    values.append(int(input()))
spans = np.array(values)
column = pd.Series(values)

# This check prints False: fix the NumPy line so that both tools give the sample std
numpy_std = np.round(np.std(spans), 2)
pandas_std = np.round(column.std(), 2)
print(f"Sample std: {numpy_std} cm, tools agree: {numpy_std == pandas_std}")

# Write your code below
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