Sample with an Explicit Rule

State the population, sample size, replacement rule, and selected indexes before applying one selection to every aligned record field.

Sampling selects some records from a larger collection. A call to a random generator is not a complete sampling rule by itself. We need to state what can be selected, how many positions to select, and whether the same position may appear more than once.

We will use six identified measurement records:

Position 0 refers to the pair ("M-101", 18.5), position 1 to ("M-102", 20.0), and so on. The IDs are stable names for records. Their array positions are the locations we will sample.

State the Sampling Rule

Suppose the rule is:

  • population: all six record positions;
  • sample size: three positions;
  • replacement: no selected position may be selected again within this sample.

NumPy expresses that rule with choice:

record_ids.size is 6, so the selectable integer positions are 0 through 5. size=3 asks for three results, giving output shape (3,). replace=False prevents a selected position from appearing again in this call.

The seed makes this particular ordered generator call replayable in the same environment. The sampling rule still needs all three other parts. A seed does not tell a reader the population, sample size, or replacement choice.

Q1. Read an explicit sampling rule

Which call selects four positions from a population of six without selecting the same position twice?

Choose one

Select one choice, then check.

HintRead one argument at a time

The first argument gives the selectable positions, size gives the output count, and replace=False forbids repeats within the sample.

SolutionSelect four of six positions without replacement

Use rng.choice(6, size=4, replace=False). Its output has shape (4,) and contains no repeated position.

Not attempted
Review

Not marked done.

Keep the Selected Indexes

The selected indexes are evidence. They show exactly which source records entered the sample and in what order. Apply the same index array to every aligned field:

If the first selected index is 4, the first sampled pair must be ("M-105", 19.0). Applying a different random choice separately to readings would break record identity even if both sampled arrays had shape (3,).

Keep IDs in the sample output or saved record. An index identifies a location in this exact source ordering; the stable ID identifies the record when the source is inspected or ordered differently later.

Q2. Apply one index array to aligned records

Given selected_indexes = np.array([4, 0, 5]), which sampled pairs preserve the original alignment?

Choose one

Select one choice, then check.

HintUse each position twice

Position 4 contains M-105 and 19.0; position 0 contains M-101 and 18.5; position 5 contains M-106 and 24.0.

SolutionPreserve the three source pairs

The aligned pairs are (M-105, 19.0), (M-101, 18.5), and (M-106, 24.0).

Not attempted
Review

Not marked done.

Decide Whether Replacement Is Allowed

With replace=False, the sample size cannot exceed the six-position population. Asking for seven distinct positions must fail because a seventh position does not exist.

With replace=True, each draw again has access to all six positions. A sample may then be larger than the population, and a position may repeat. Repetition is allowed, not guaranteed. The output shape still follows size:

Choose replacement from the task. Do not switch it merely to make an invalid sample size run. This lesson treats all positions alike; weighted sampling and claims about a wider population require ideas taught elsewhere.

Q3. Sample aligned records and check the rule

Complete the program so it samples three indexes without replacement and uses that one index array for both aligned fields.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintSelect positions before values

Use rng.choice(record_ids.size, size=3, replace=False). Then index both record_ids and readings with selected_indexes.

SolutionKeep one selection record

The selected indexes have shape (3,), contain three distinct positions, and preserve each ID–reading pair.

Not attempted
Review

Not marked done.

An explicit sampling rule states the population, sample size, and replacement choice, then keeps the selected indexes. Apply that one index array to every aligned field and retain stable record IDs. The next lesson uses one complete permutation to reorder and split all six records without breaking pairs.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.