Skip to the patient leakage demo

Medical AI lab · Patient leakage ·

Patient-level data leakage: interactive demo

Patient-level data leakage is when visits from the same patient land in both the training and the test set, so the model scores well by recognising patients rather than learning the outcome. In this demo the same 7-nearest-neighbour model has a reported test AUC of 0.98 when the synthetic records are split by visit, 0.76 when they are split by patient, and 0.72 on 240 patients it has never seen. Splitting by patient is what makes the test score honest.

In one line: Split by record the reported test AUC is 0.98; split by patient it is 0.76; on new patients the same model scores 0.72.

SYNTHETIC DATA

These demos are illustrative. Every signal, image, patient and note is synthetic and fictional, generated and scored in your browser, and every number shown is computed from that synthetic data. They show general principles, not the performance of any real model, product or project, and none of them is a medical device.

Nothing is uploaded. No patient data is used.

02

Clinical model validation / data splits

Same model. Two test scores.

What this shows: split by record, a patient’s other visits sit in the training set, and the model scores well by recognising patients. Split by patient, the test score matches what it does on new patients.

Split the data

Assign to the test set

300 synthetic patients, 30% with the outcome; 25% of records, or of patients, go to the test set. The model is a 7-nearest-neighbour classifier on 8 features. Try one visit per patient: the two splits then agree.

Who is in the test set

first 12 of 300 patients

Grey: training recordBlue: test record

Bars start at AUC 0.5, chance level. New patients: 240 synthetic patients the model never saw.

Reported test AUC—
AUC on new patients—
Optimism—

Synthetic data · illustrative · not a medical device

Where this matters

Any clinical dataset with repeated visits, admissions, images or samples per patient. The split has to follow the unit the model will meet in practice: new patients, and often new sites or later time periods.

What this experiment models

Each synthetic patient has a stable set of 8 features, a fingerprint, and a single outcome that shifts 3 of those features slightly. Each visit adds small visit-to-visit noise to the fingerprint, so a patient’s visits are much more alike than two different patients. A distance-weighted 7-nearest-neighbour classifier is scored with the area under the ROC curve (AUC). The data is fixed by a seed, so the same settings always give the same numbers.

The principle

How it works

Each of the 300 synthetic patients has a stable set of eight features and one outcome, which shifts three of the features slightly. Every visit repeats the patient’s features with a little noise, so two visits of one patient are far more alike than two patients with the same outcome. Real records behave the same way: age, chronic conditions and baseline measurements carry over from visit to visit.

The model is a k-nearest-neighbour classifier, which predicts from the most similar training records. Split by record, a test visit’s nearest neighbours are usually the same patient’s other visits, which carry the right label, so the test AUC climbs towards 1 as visits per patient increase. Split by patient, every test patient is new to the model, and the test AUC stays close to the AUC on 240 fresh patients. With one visit per patient the two splits are equivalent.

Flexible models such as gradient boosting and neural networks can memorise patients in the same way. The fix is in the evaluation design: split by patient, and by site or time period where the model will meet new ones. See clinical model validation.

Worked example

A readmission model with a test score that is too good

Picture a team validating a readmission model. Their table has one row per admission, and many patients have several admissions. They shuffle the rows, hold out a quarter as the test set and train on the rest. This is the default in most machine-learning tutorials, and nothing about it looks wrong.

The demo’s default settings reproduce that setup on synthetic data: 300 patients with 4 visits each, split at random by record into 903 training and 297 test records. The reported test AUC is 0.98. Under the readouts the demo adds one more figure: 100% of the test records belong to a patient who also has records in training.

Then the model meets patients it has never seen. On 240 fresh synthetic patients the same model has an AUC of 0.72, so the test score was optimistic by +0.26. The model had not learned much about the outcome. For each test visit, its nearest neighbours in the training set were the same patient’s other visits, which carry the same label, so it was looking patients up.

Now look at the second pair of bars, where the same records are split by patient: 880 training and 320 test records, and 0% of test records share a patient with training. The reported test AUC is 0.76 against 0.72 on new patients, an optimism of +0.04. The model is no better and no worse than before. Only the estimate has changed, and it is now close to what deployment will show.

The correct practice is to decide which unit has to be new at deployment, usually the patient, and to keep all of that unit’s records on one side of every split. That applies to cross-validation folds and hyperparameter tuning as well as to the final test set, and to preprocessing steps, which should be fitted on training data only. Where the model will also meet new sites or later time periods, hold those out too.

Checklist

What to check in your own project

  • Which unit has to be new at deployment: the patient, the site, the time period? Is the split made on that unit?
  • Is there a reliable patient identifier, and were duplicate or merged records resolved before splitting?
  • Does each patient appear in exactly one of training, validation and test, including in cross-validation folds and tuning?
  • Were scaling, imputation and feature selection fitted on the training data only?
  • Is any feature recorded after the moment of prediction, or derived from the outcome?
  • Has the model been tested on a later time period or another site, not only on a random hold-out?

Related: our clinical model validation service, the wiki page on medical AI validation, and the demos on calibration and decision thresholds and scanner shift.

FAQ

Common questions

How do I detect patient-level data leakage?

Check whether any patient has records on both sides of a split: count the share of test records whose patient also appears in training. In the demo that share is 100% for a random split by visit and 0% for a split by patient, and the reported test AUC drops from 0.98 to 0.76 when the leakage is removed. A test score far above performance on genuinely new patients is the other warning sign.

Does data leakage affect cross-validation as well as the test set?

Yes. Every fold, every hyperparameter search and every preprocessing step fitted on training data has to respect the same patient-level grouping, or the tuning will pick settings that exploit the leak. Use a grouped cross-validation that keeps all of a patient’s records in one fold.

How much does patient leakage inflate the AUC?

It depends on how alike a patient’s records are and how many each patient has. In this synthetic demo with 4 visits per patient the record-level split reports an AUC of 0.98 against 0.72 on new patients, an optimism of 0.26; with one visit per patient the two splits agree. Real datasets vary, so measure it on your own data with a patient-level hold-out.

What is data leakage in machine learning?

Data leakage is information reaching the model during development that will not be available when it is used, which makes test results look better than real performance. Common forms are the same patient appearing in both training and test data, features recorded after the outcome, and preprocessing fitted on the full dataset before splitting.

Should I split clinical data by patient or by record?

By patient, whenever one patient can contribute more than one record. Records from the same patient are much more alike than records from different patients, so a record-level split lets the model recognise people. In this demo the record-level split reports an AUC of 0.98 for a model that scores 0.72 on new patients, while the patient-level split reports 0.76.

How do I do a patient-level train/test split?

Assign patients, not rows, to the training and test sets: draw the split over the list of patient identifiers and send all of a patient’s records the same way. Most libraries call this a grouped split; scikit-learn, for example, has GroupShuffleSplit, GroupKFold and StratifiedGroupKFold. Use the same grouping for cross-validation and hyperparameter tuning.

Why is my model’s test AUC much higher than its performance on new patients?

Leakage in the original evaluation is the first thing to rule out: check whether test patients, or near-duplicates of their records, also appear in the training data. If the split is clean, the cause is more likely a real difference between the development data and the new patients, such as a different site, period, case mix or measurement method.

Medical AI lab

More demos

01 / Heart rate from ECGHeart rate from a noisy ECG – interactive demoMissed and false beats behind a correct average03 / Calibration & thresholdsCalibration and decision thresholds – interactive demoCalibration plot, decision curve and net benefit04 / Scanner shiftScanner shift in medical imaging – interactive demoSegmentation tuned on one scanner, tested on another05 / Dice vs clinical errorDice score vs clinical error – interactive demoDice, HD95 and lesions detected06 / Note de-identificationDe-identifying a clinical note – interactive demoEnglish, Hebrew and mixed notes vs a rule-based detector07 / Nodule volume 3DNodule volume and CT slice thickness – 3D interactive demoThe same nodule, measured on thin and thick slices08 / Sample sizeSample size for validating a clinical AI model – interactive demoHow wide the interval is around a measured sensitivity09 / SubgroupsHidden subgroup failure in clinical AI – interactive demoA good overall score next to a weak subgroup10 / Slice-by-slice 3DSlice-by-slice segmentation of a 3D lesion – 3D interactive demoA high per-slice Dice and a lesion reported in pieces11 / Predictive value 3DPositive predictive value and prevalence – 3D interactive demoWhat a positive result means when the condition is rare12 / Shortcut learningShortcut learning in medical imaging AI – interactive demoA model that reads the hospital tag, not the disease13 / DriftModel drift and performance monitoring – interactive demoHow late a monitoring rule notices a slow decline14 / Alert burdenAlert burden of a clinical early-warning model – interactive demoA threshold counted in alerts per nurse per shift

MLAIA DATA SCIENCE

Tell us what
you’re working on.

Share the problem, the data you have and what success would look like. We’ll discuss a practical next step.

Please don’t include confidential datasets, credentials or patient information.

yochai@mlaia.com
WhatsApp: +972 52 484 6282

Your inquiry is handled under our Privacy Policy.