Bigger Samples, Less Noise
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 22 of 64.
Bigger samples give better estimates, but how much better? Here are 1000 means of 25 shopping baskets, and 1000 of 100 baskets:
sample_means measures each spread. The standard error, std / np.sqrt(n), predicts it with no simulation:
for n in [25, 100]:
spread = sample_means(baskets, n, 1000).std()
print(n, np.round(spread, 2), np.round(baskets.std() / np.sqrt(n), 2))This prints:
25 3.43 3.45
100 1.72 1.72Four times the data, half the noise: that is the square root at work. A model tested on 400 examples has a score twice as precise as on 100.
Challenge
Mediumbags holds the weights of a mill's 5000 flour bags, in grams. Quality control weighs a sample hourly; the program reads the standard error it accepts.
- Find the smallest
nwhose standard error,bags.std() / np.sqrt(n), is at most that: start at 1, add 1 while too large. - Check it: the std of 300 sample means from
sample_means. - Print as below, to 2 decimals.
For another mill and 2.5:
Bags to weigh: 24
Standard error: 2.46 g
Simulated spread: 2.37 gTry it yourself
import numpy as np
target = float(input())
# Don't change the lines below: the weight in grams of every bag of flour
rng = np.random.default_rng(22)
bags = np.round(rng.normal(500, 8, size=5000), 1)
# Your function from Sample Means Vary: the means of count samples
def sample_means(data, size, count):
means = []
for i in range(count):
sample = rng.choice(data, size=size, replace=False)
means.append(sample.mean())
return np.array(means)
# 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