Shuffling and Splitting

Many experiments shuffle examples before splitting them into groups.

A common split is:

train examples
test examples

The train set is used to fit the method. The test set is held back for a final, unbiased check. If you repeatedly compare alternatives or tune settings, use a separate validation set rather than choosing based on test results.

Shuffle indices

Instead of shuffling the data directly, start by shuffling indices.

Shuffle and split indices

Runs locally with Python in your browser.

Ready to run.

The seed makes the split repeatable.

Keep paired arrays paired

If X contains examples and y contains labels, do not shuffle them separately. Shuffle one set of indices, then use those indices for both.

This keeps each example with its label.

Exercise: Keep pairs together

Why is it safer to shuffle indices and apply them to both X and y?

Choose one

Select one choice, then check.

Hint

The same index must select an example and its corresponding target.

Solution

One shared permutation keeps each example with its label. Shuffling X and y independently would usually destroy that pairing.