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.54Both add the same squared deviations, then divide:
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
EasyA 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.
- Bats are a sample: fix the NumPy line so both tools give the sample std.
- Below it, print how far NumPy's default
np.std(spans)falls belowcolumn.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 cmTry 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
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