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.

test accuracy, random split of tiles
test accuracy, split by patient
actual accuracy on 40 new patients

2. What just happened

The rule: split at the level you want to generalise to. If the claim is "works on new patients", no patient may appear on both sides. If the claim is "works at new hospitals", hold out whole hospitals.

3. Where leakage hides

SituationWhy it leaksFix
Tiles or fields of view from one slide in both setsShared staining, focus and tissueGroup by slide, better by patient
Frames of one time-lapse movie in both setsNeighbouring frames are nearly identicalGroup by movie or by experiment day
Augmenting before splittingRotated copies of a training image land in the test setSplit first, augment the training part only
Normalising or selecting features on all dataTest statistics shape the training inputFit every preprocessing step on the training part only
Tuning on the test setEach look at the test score leaks a little informationKeep a validation set for tuning and touch the test set once
All diseased samples from site A, all healthy from site BThe model learns the site, not the diseaseBalance 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.