liveBook · Manning

Shuffle

As explained in Deep Learning with Python, Third Edition

To randomly reorder data before splitting it so that contiguous ordering does not bias the partition.

Shuffle randomly reorders examples before splitting or training. This prevents contiguous ordering—such as grouping images by object type or digits by class—from biasing the data partition. section 12.2.1 section 5.2.3

Avoiding an ordered split

If digit samples are arranged by class, taking the first 80% for training can leave classes 0–7 in training and classes 8–9 in testing. Randomly shuffling before splitting produces more representative partitions. section 5.2.3

Use shuffle with these practical rules:

  • Shuffle before ordinary training/test or training/validation splits when the data order is not meaningful. section 5.2.3
  • Do not shuffle forecasting data whose order represents time; otherwise, future examples can leak into training. section 5.2.3
  • Remove duplicate or redundant examples before splitting so shuffled training and validation sets remain disjoint. section 5.2.3
  • For streaming datasets, use .shuffle(buffer_size) to shuffle elements within a buffer. section 8.2.4
import random

metadata = list(filter(lambda x: len(x["boxes"]) <= 4, metadata))
random.shuffle(metadata)

A compact COCO preparation example filters images by annotation count and then calls random.shuffle(metadata) before model training. section 12.2.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains the operation, demonstrates Python and Dataset APIs, and covers temporal leakage and duplicate-data caveats.
Examples
Demonstrates ordered digit classes, COCO images grouped by object type, Python random.shuffle, and Dataset .shuffle(buffer_size).
What it assumes
Assumes familiarity with datasets and train/test splits, while clarifying why accidental ordering can invalidate evaluation.
What you can do after
Apply it via random.shuffle or Dataset.shuffle while avoiding temporal leakage and overlapping evaluation data.
How it connects
Connects data shuffling with data splitting, training sets, validation sets, test sets, temporal leakage, and data redundancy.

Also explained in

Compare how 3 books explain shuffle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.