Start learning free

LESSON 122 OF 165 · MACHINE LEARNING: STATISTICS AND A/B TESTS

A Sample Is Not the Truth

Practice

The shop has 300 customers, and their average spend is 189.9. Often you can't ask everyone, so you look at a sample: a few customers picked at random. Its average is close to the true one but rarely equal, and another pick gives another average.

rng.choice(values, size=n, replace=False) gives back n values picked at random from values; replace=False never picks the same position twice. The rng made by np.random.default_rng(0) picks the same way on every run, and every call moves on to new random numbers.

ages = np.array([18, 25, 31, 40, 52, 67])
pick = rng.choice(ages, size=3, replace=False)   # three different ages
print(pick.mean())                               # their average

The bigger the sample, the less its average moves: one big spender is 1 of 30 values in a small sample, but only 1 of 150 in a big one. Your script draws five samples of 30 customers and five of 150, and prints their averages below the true one:

All 300 customers: 189.9
30 customers: 194.8 | 150 customers: 184.9
30 customers: 206.3 | 150 customers: 190.3
30 customers: 204.0 | 150 customers: 196.6
30 customers: 169.5 | 150 customers: 185.9
30 customers: 176.1 | 150 customers: 192.1

The averages of 30 customers land anywhere from 169.5 to 206.3; those of 150 stay between 184.9 and 196.6.

Your task: write a function sample_mean(size) that draws size customers' spends at random, without replacement, and gives back their mean rounded to 1 decimal. Then print five lines, each with the mean of a sample of 30 and of a sample of 150.


Assignment
Follow the Coding Tutorial to draw random samples of customers and compare their average spend with the whole shop's.


Hint
spent is already a numpy array and rng is already made: use them, don't make a new rng. On each line, call sample_mean(30) before sample_mean(150), so the random picks come in the same order as the expected output.


STEP 1 OF 5
    Ctrl + Enter to check