Sam Newby
← Learning ML

Data preparation

Shuffling a dataset correctly matters

Something I’ve been learning while building ML models from scratch is just how important data preparation is. Take something as simple as shuffling a dataset.

You have your inputs (x) and their corresponding expected outputs (y). Shuffle them independently, and suddenly your model is learning from inputs paired with the wrong answers.

My mistake was thinking it would be an obvious error to spot. Training and validation ran fine and my code didn’t crash but my predictions and loss were way off.

The solution is simple: generate one random permutation and apply it to both arrays.

rng = np.random.default_rng(42)
p = rng.permutation(len(X))

X = X[p]
y = y[p]

Four lines of code that I got wrong and my model was doomed from the start.