Menu
CoddyTech

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:

40 dots, one per sample of 30 parcels, scattered between about 7.2 and 8.8 kg around a dashed line at the true mean of all parcels, 8.05 kg

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 icon

Challenge

Medium

A 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 cm

Try 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")
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