AI for Doctors / Theory / Lesson 2

How a machine learns, and how it fools itself

Training, testing, overfitting and data leakage, with a model you can bend until it breaks.

12 min

Supervised learning is the commonest kind in medicine. You give the computer many examples where the answer is known (X-rays labelled “pneumothorax” or “normal”) and it adjusts itself until its guesses match the labels as closely as possible. The gap between its guess and the true label is the error, and training is the process of shrinking that error.

The trap is that a flexible enough model can shrink the error to zero by memorising the training cases, noise and all. It then does badly on new patients. Try it:

Make the model more flexible

Big teal dots: 12 training patients. Faint orange dots: 40 new patients the model never saw. Purple line: the model.

Error on training data0.108
Error on new patients0.072

Too simple: misses the real pattern (underfitting).

This is why every honest study splits its data three ways. The training set teaches the model. The validation set is used to tune it (choose the flexibility, the cut-off). The test set is locked away and used once, at the end, to report performance.

Data leakage

If the same patient appears in both training and test sets (say, two X-rays from one admission), the model can recognise the patient rather than the disease, and the test score looks better than it is. Splits must be done at patient level. Other leaks: a variable that is only recorded after the diagnosis (an antibiotic order used to “predict” sepsis), or a scanner label burned into the image.

Supervised learning: learning from labelled examples.
Unsupervised learning: finding groups or patterns in data with no labels, such as clustering patients into phenotypes.
Reinforcement learning: learning by trial, error and reward. Used to tune chatbots with human feedback.

Watch

How a machine learns: fitting by reducing error
5 min · narrated
Overfitting
4 min · narrated
Train, validation and test split, and leakage
4 min · on-screen captions, no voice

Check yourself

1. A model scores 99% on its training data and 70% on new patients. The likely problem is:
2. What is the test set for?
3. Two X-rays from the same patient end up in both training and test sets. This is:

Educational material, not clinical advice. Interactive numbers are teaching examples unless a source is named.