AI · 10 minutes
Split it right
Biomedical data come in groups: many image tiles per slide, many slides per patient, many patients per hospital. Split them at random and your test set is full of near-duplicates of the training set. This simulation shows how large the illusion can get.
Start here for readers new to machine learning
- Slide, tile
- A tissue sample on glass is scanned into one enormous image, far too large for a model. It is cut into thousands of small square tiles, and the model looks at one tile at a time.
- Training and test set
- A model learns from one part of the data (training) and is then scored on a part it has never seen (test). The test score is the only evidence that it will work on new cases, so the test data must be truly new.
- Accuracy
- The share of test examples the model gets right. With two equally common classes, 50 % is a coin flip.
- Nearest-neighbour classifier
- The simplest model there is: for a new example, find the most similar training example and copy its label.
1. Run the experiment
Each simulated patient contributes several image tiles. Every tile carries a personal signature of its patient (staining, scanner, anatomy) and, for diseased patients, a disease signal. A nearest-neighbour classifier predicts disease from a tile.
Two of twelve feature dimensions. One colour per patient. Filled: diseased. Hollow: healthy. The disease signal acts along the horizontal axis.
2. What just happened
- With a random split, almost every test tile has a sibling from the same patient in the training set. The classifier finds that sibling and copies its label. It has learned to recognise patients, which is useless for the next patient.
- Splitting by patient removes the shortcut. The estimate drops, and it now agrees with what happens on patients the model has never seen.
- Set the disease signal to zero. The random split still reports high accuracy on data that contain nothing to learn. Deep networks are even better at memorising signatures than this simple classifier.
3. Where leakage hides
| Situation | Why it leaks | Fix |
|---|---|---|
| Tiles or fields of view from one slide in both sets | Shared staining, focus and tissue | Group by slide, better by patient |
| Frames of one time-lapse movie in both sets | Neighbouring frames are nearly identical | Group by movie or by experiment day |
| Augmenting before splitting | Rotated copies of a training image land in the test set | Split first, augment the training part only |
| Normalising or selecting features on all data | Test statistics shape the training input | Fit every preprocessing step on the training part only |
| Tuning on the test set | Each look at the test score leaks a little information | Keep a validation set for tuning and touch the test set once |
| All diseased samples from site A, all healthy from site B | The model learns the site, not the disease | Balance sites across classes, or hold out a site |
4. Check yourself
Notes and caveats
The simulation draws Gaussian features in twelve dimensions. Each patient has a random offset (the signature), diseased patients are shifted along one axis (the signal), and each tile adds unit noise. The classifier is 1-nearest-neighbour, chosen because it makes memorisation visible. Accuracies are averaged over several random splits and still fluctuate by a few points. Real data are messier, but the effect is the same and is well documented in the medical imaging literature.