Sampling

Sampling means choosing values from a set of possible values.

NumPy uses choice for many sampling tasks.

rng.choice(items, size=3)

Sample from a list

Sample labels

Runs locally with Python in your browser.

Ready to run.

By default, choice samples with replacement. That means the same item can appear more than once.

Sample without replacement

Use:

rng.choice(items, size=3, replace=False)

when each chosen item should appear at most once.

Probabilities

You can pass probabilities:

rng.choice(labels, size=6, p=[0.6, 0.3, 0.1])

The probabilities must add to 1.

Exercise: Without replacement

Which argument tells choice not to repeat selected items?

Answer it first, then check.

Hint

Set the argument that controls whether a selected item is put back.

Solution

Use replace=False. A selected item is not returned to the pool, so it cannot be selected again in that sample. The requested sample size must not exceed the number of available items.