Sample Means Vary
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 20 of 64.
One sample of 30 parcels gives one estimate of their mean weight. Draw again and you get another. Here are 40 samples:
To collect them, repeat the sampling in a loop, append each mean to a list, then make the list an array:
means = []
for i in range(5):
sample = rng.choice(parcels, size=30, replace=False)
means.append(sample.mean())
means = np.array(means)
print(np.round(means, 2))This prints:
[7.98 8.05 8.12 8.19 8.76]A sample mean is itself random, and means.std() measures how much it moves. A model's test score is one such mean: another test set would shift it.
Challenge
MediumA gardener measures a random sample of a nursery's 1800 bamboo plants each week. How much can the sample mean jump between weeks?
Complete sample_means(data, size, count): draw count samples of size values from data, no value twice in a sample, and append each sample's mean. The starter's last lines call it and print the report.
For another nursery and samples of 10:
200 means of 10 plants: 105.25 to 137.13 cm
Spread of the means (std): 5.27 cmTry it yourself
import numpy as np
size = int(input())
# Don't change the lines below: the height in cm of every bamboo plant
rng = np.random.default_rng(20)
heights = np.round(rng.normal(95, 15, size=1800), 1)
def sample_means(data, size, count):
means = []
# Write your code below: count samples of size values, keep each mean
return np.array(means)
# Don't change the lines below
means = sample_means(heights, size, 200)
print(f"200 means of {size} plants: {np.round(means.min(), 2)} to {np.round(means.max(), 2)} cm")
print(f"Spread of the means (std): {np.round(means.std(), 2)} cm")
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