Shuffle and Split Paired Records

Reuse one permutation across every aligned field, split at an explicit boundary, and verify pair identity, disjointness, and complete coverage.

Randomly reordering one array is easy. Reordering several aligned arrays without breaking their records requires a stronger rule: generate one permutation of source positions, then apply it everywhere.

We will keep the same six records:

Every position joins one stable ID with one reading. M-104, for example, must remain paired with 22.0 after reordering and splitting.

Generate One Permutation of Positions

A permutation contains every source position exactly once, in a new order:

The exact order is generator output. Its required properties are stable:

print("all positions once:", np.array_equal(np.sort(order), np.arange(6)))
all positions once: True

Apply this one order to both fields:

At every shuffled position, the ID and reading come from the same source position. Calling rng.permutation separately for each field would create two different orders and silently break the pairs.

Q1. Reuse one permutation

Which program preserves the alignment between record_ids and readings?

Choose one

Select one choice, then check.

HintTreat positions as the shared record

Generate the positional decision once. Reuse it for every aligned array.

SolutionApply one order everywhere

Both shuffled arrays now refer to the same sequence of source positions.

Not attempted
Review

Not marked done.

Distinguish a Returned Permutation from In-Place Shuffle

rng.permutation(6) returns a new array of the positions 0 through 5. It does not change record_ids or readings.

rng.shuffle(array) has a different interface. It changes the supplied array in place and returns None:

Use permutation when a returned order is the evidence that several arrays must share. In-place shuffle can be suitable when changing one owned array is the intended action, but it is easy to misuse when several fields must stay aligned. In either case, the generator's state advances.

Q2. Read the shuffle interface

After returned = rng.shuffle(positions), where is the shuffled order stored?

Choose one

Select one choice, then check.

HintFollow the phrase in place

An in-place method changes the object passed to it. Its return value is not a second copy of that object.

SolutionInspect the mutated array

Read the new order from positions. returned is None.

Not attempted
Review

Not marked done.

Split the Permuted Indexes at an Explicit Boundary

Suppose four records form a working group and two are held back as a checking group. Split the index permutation before selecting the fields:

The boundary 4 states the group sizes. These neutral names describe how the records will be used here. Later subjects define domain-specific splitting roles and policies.

Two checks establish the positional split. First, no checking index should appear in the working indexes:

Second, the two groups together should cover all source positions. Introduce np.concatenate for this exact need: it joins the two one-dimensional index arrays end to end.

Sorting the recombined positions puts them back into source order. Full coverage therefore means the result is exactly np.arange(6).

Disjointness prevents one position from entering both groups. Coverage prevents a position from disappearing. Neither check alone proves pair identity, so inspect the IDs and readings produced by the same group indexes.

Q3. Build and check an aligned split

Complete the program with one permutation, a four-record working group, a two-record checking group, and the stated disjointness and coverage checks.

Editable Python

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

Ready to run.

HintSplit indexes before selecting fields

Set order = rng.permutation(record_ids.size), then use order[:4] and order[4:]. Apply each of those arrays to both record_ids and readings.

SolutionReuse one split of one order

The existing checks then report working size 4, checking size 2, no overlap, full coverage, and preserved pairs in both groups.

Not attempted
Review

Not marked done.

Generate one positional permutation and reuse it for every aligned field. Split that order at an explicit boundary, then check disjointness, coverage, and pair identity. The next lesson records the generator and sampling choices beside the measurements and saved artifacts they produced.

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.