Machine learning, step by step
Watch a model learn, then try to break it.
Four clinical problems, four different techniques. Every model here is trained live in your browser, one step at a time, and every step has something to drag, tap or switch.
-
Chapter 1 · Logistic regression
Predicting diabetes risk
Fit a risk score by hand, watch gradient descent do it better, choose a clinical threshold, then see what happens at another hospital.
Supervised · loss · gradient descent · ROC · calibration
-
Chapter 2 · Decision tree
Sepsis triage rules
Learn a qSOFA-style rule from vital signs one yes/no question at a time, and find the point where more questions make it worse.
Gini impurity · greedy splits · overfitting · interpretability
-
Chapter 3 · k-means clustering
Finding diabetes subgroups
No labels this time. Drop cluster centers, step through the algorithm, pick the number of groups, and see why units matter.
Unsupervised · inertia · elbow · silhouette · scaling
-
Chapter 4 · Neural network
Reading heartbeats
Turn an ECG beat into numbers, design a neuron by drawing it, train a network, then draw beats to fool it.
Neurons · backpropagation · validation · saliency
-
Wrap-up
Before it touches a patient
What each chapter shows about validating, deploying and monitoring clinical models.
External validation · fairness · drift · reporting
Every patient on this site is simulated with a few lines of code. None of it is sent anywhere: the data, the training and the predictions all run inside this page, which only records an anonymous visit count. It's a teaching tool, not a clinical one.
Chapter 1 · Logistic regression
Predicting diabetes risk
A clinic wants to find adults at high risk of type 2 diabetes so it can offer a prevention program. We'll build the model it would use, one step at a time.
Learning from labeled examples
Each dot is a simulated adult without diabetes who had a glucose tolerance test: their 2-hour glucose across, their BMI up. Orange patients were diagnosed with type 2 diabetes within five years; blue ones weren't.
That known outcome is the label. Supervised learning looks for a rule that maps the features to the label and still works on patients it has never seen.
We've also locked away 150 more patients for testing. The model never learns from them; we'll use them in step 4 to see how it really does.
Training patients
A risk score you can build with sliders
Logistic regression is the workhorse behind many clinical risk calculators. It multiplies each feature by a weight, adds a baseline, and gets a score z. Then the sigmoid squashes z into a probability between 0 and 1.
Features are standardized first (measured in standard deviations from the average), so the two weights are directly comparable.
The shading is the predicted risk. The line is where the model says 50%.
Log loss grades the predictions: for each patient, −log of the probability the model gave to what actually happened. Confidently wrong costs a lot. Confidently right costs almost nothing.
The sigmoid
Every patient placed by their score z
Let the computer turn the knobs
Sliders don't scale to 30 features. Gradient descent does it automatically: work out which way each weight should move to lower the loss (the gradient), take a small step that way, and repeat.
The map shows the log loss for every pair of weights, with the baseline held at its current value. Stronger blue is lower. Training is a ball rolling downhill.
The learning rate is the step size. Too small and it crawls. Too large and it overshoots, bouncing across the valley or flying out of it.
It starts from wherever you left the sliders in step 2.
Model
Shading and 50% line update as it learns
Loss landscape
Tap to move the starting point
Log loss over training steps
From probability to decision
The model gives a probability. The clinic needs a yes or no: offer the program or not. Where to draw that line is a clinical judgment, not a math one.
These are the 150 held-out patients, scored by the fully trained model. Patients who developed diabetes stack above the axis; those who didn't hang below. Everyone right of the threshold gets flagged.
- Sensitivity: of those who developed diabetes, the share we flagged.
- Specificity: of those who didn't, the share we left alone.
- PPV: of those flagged, the share who really developed it.
The ROC curve traces every threshold at once. The area under it (AUC) is the chance the model ranks a random future case above a random non-case.
Predicted risk, held-out patients
Confusion matrix
ROC curve · AUC –
Same model, different patients
The model is deployed at a second hospital. Nothing about the model changes, but the patients do.
Calibration asks: when the model says 30%, do about 30% go on to develop diabetes? Points on the diagonal mean yes. A risk calculator that's miscalibrated tells patients the wrong number, even if it ranks them in the right order.
This is why clinical models need external validation and monitoring after deployment, not just a good score on the data they were built from.
Calibration
Performance at threshold –
| Original | New site |
|---|
Chapter 2 · Decision tree
Sepsis triage rules
Emergency departments use short checklists like qSOFA to spot patients with infection who might crash. A decision tree learns a checklist like that from data.
Split the patients in two
These are 300 simulated emergency patients with suspected infection. Respiratory rate across, systolic blood pressure up. Orange patients deteriorated within 48 hours (ICU transfer or death).
A tree asks one yes/no question about one feature, then splits. A good question leaves each side purer: closer to all orange or all blue.
Gini impurity measures the mix. It's 0 when a group is all one outcome and 0.5 at a 50/50 split. We want the split with the lowest impurity, averaged over both sides and weighted by their sizes.
The two sides
Impurity after the cut
Weighted Gini at every threshold · drag here too
Then ask again, and again
After the first split, the tree repeats the same search inside each half, then inside those halves. This is greedy: every split is the best one available right now, with no look-ahead.
Each shaded box on the chart is a leaf: patients who gave the same answers to every question. A leaf's predicted risk is just the share of its patients who deteriorated. Darker orange means higher risk.
The tree
Each node: the question it asks, how many patients reach it, and their deterioration rate
Memorizing isn't learning
Keep splitting and the tree eventually walls off tiny boxes around single patients. It has memorized the training set, noise included, and gets worse on patients it hasn't seen. That's overfitting.
The chart compares AUC on the 300 training patients and on 120 held-out patients as the tree gets deeper. Training AUC only goes up. Held-out AUC peaks and then falls. The gap is the overfitting.
Minimum leaf size is a guardrail: no leaf may hold fewer patients than this.
Leaves on the training data
AUC by depth
Why was this patient flagged?
A shallow tree can be read like a protocol. That's a big part of why rules like this show up in clinical decision support: a clinician can see exactly why the alert fired and judge whether it makes sense.
Here's a depth-3 tree trained on all 300 patients. It found its own cutoffs, which you can compare with the ones in qSOFA: respiratory rate ≥ 22 and systolic BP ≤ 100 (qSOFA also uses altered mental status, which isn't in this data).
The tree
The same tree as rules
Chapter 3 · k-means clustering
Finding diabetes subgroups
In 2018, a Scandinavian study clustered thousands of newly diagnosed adults and argued that "type 2 diabetes" is really several subgroups with different risks. Here's the core idea, on simulated patients.
No labels this time
Each dot is a simulated patient at diabetes diagnosis: age across, HbA1c (average blood sugar over ~3 months) up. There are no colors because there are no answers. Unsupervised learning looks for structure on its own.
k-means describes each group by a center. Every patient belongs to whichever center is closest. The shading shows each center's territory.
Inertia is the total squared distance from each patient to their center. Small inertia means tight groups; k-means tries to make it as small as possible.
Assign, move, repeat
k-means alternates two moves:
- Assign every patient to their nearest center.
- Move each center to the average of its patients.
Neither move can increase the inertia, so the algorithm always settles. It doesn't always settle in the best place, though: where it ends up depends on where it starts.
It starts from the centers you dropped in step 1.
Inertia after each step
Choosing k
k-means will find exactly as many groups as you ask for, even in pure noise. Choosing k is part of the analysis, not something the algorithm answers.
The elbow plot shows inertia for k = 1 to 6 (best of 10 runs each). It always falls as k grows; look for the bend where adding a group stops helping much.
The silhouette score asks whether patients sit closer to their own group than to the next nearest one. It runs from −1 to 1; higher is better.
Clusters are a hypothesis. The 2018 subgroups mattered because they went on to predict different complication rates, not because an algorithm drew them.
Elbow: inertia by k
Silhouette by k
k = 4
Distance depends on units
In this data, age spans about 60 years while HbA1c spans about 7 percentage points. Measured raw, a 1-year age gap counts the same as a 1-point HbA1c gap. Clinically, that's absurd: one HbA1c point is a big difference in blood sugar.
Standardizing measures each feature in its own standard deviations, so both get a fair say. Everything in steps 1 to 3 used standardized units.
Because this data is simulated, we know the groups we built in, so we can check which version finds them.
Chapter 4 · Neural network
Reading heartbeats
Bedside monitors raise hundreds of alarms per patient per day, and most are false. We'll train a small neural network to label single heartbeats as normal, a premature ventricular contraction (PVC), or motion artifact.
What the network actually sees
A network doesn't see a trace. It sees numbers. Each beat window is sampled at 64 evenly spaced moments, and each sample becomes one input.
- Normal: small P wave, narrow spike (QRS), rounded T wave.
- PVC: no P wave, a wide, bizarre QRS, and a T wave pointing the opposite way.
- Artifact: the patient moved or a lead slipped. No real heartbeat shape.
The strip below the trace is the same beat as the network receives it: 64 numbers, colored by value.
The input vector
Blue below zero, red above · hover or tap for values
A neuron is a template
A neuron multiplies each input by a weight and adds up the results. Lay the 64 weights out in time and they form a template. The sum (the neuron's activation) is large when the beat lines up with the template.
The middle chart shows each input times its weight. The activation is the sum of those bars.
The bottom chart runs this one neuron over 630 training beats. If the three types land in separate places, the neuron has found a useful feature.
Input × weight · activation –
This neuron on 630 training beats
Many neurons, learned together
Our network has one hidden layer of neurons, each learning its own template, and an output layer that weighs their votes into a probability for each beat type.
Training is the same gradient descent as chapter 1. The error is passed backward through both layers to work out how every weight should change (backpropagation).
Validation loss is measured on 270 beats the network never trains on. If it starts rising while training loss keeps falling, the network is memorizing.
It trains on 1,400 simulated beats. Each epoch is one pass through all of them.
Loss by epoch
Validation confusion matrix
Hidden neuron templates
Each neuron's 64 input weights. The bars on the right are its votes for normal, PVC and artifact.
Stress-test the model
A model that scores well on validation can still fail on inputs unlike anything it trained on. Poke at it.
The shaded bars behind the trace are a saliency map: how much the prediction changes when each sample changes. It's a common sanity check that a model looks at the QRS and T wave rather than, say, the baseline.
- Widen the QRS on a normal beat. To a clinician that's a warning sign. Does the network care? PVCs in its training data also lack a P wave and have a flipped T wave, so it may have learned those shortcuts instead.
- Add baseline wander, which is common and harmless. Does a normal beat start reading as artifact?
- Try a flat line. It isn't any of the three classes, but the network has to pick one anyway. It has no way to say "I don't know."
Drag on the chart to draw.
Network says
Wrap-up
Before it touches a patient
Every chapter hid a lesson about clinical deployment. A model that trains well is the beginning of the work, not the end.
-
Test on patients the model never saw
Held-out data catches overfitting; data from another hospital, era or device catches much more. A deep tree looked perfect on its training data and got worse on new patients. -
Check calibration, not just AUC
At the new hospital, AUC didn't move while every predicted risk was wrong. Patients and clinicians act on the number, so the number has to be right. -
Choose thresholds with the people who live with them
Sensitivity, alert burden and missed cases trade off against each other. The right threshold depends on what a false alarm and a miss actually cost, which is a clinical and ethical call. -
Look inside subgroups
A model can be accurate overall and poor for a particular age group, sex, ethnicity or site, especially if that group was rare in training. Report performance for each one. -
Watch for leakage
Features that quietly encode the answer (a test clinicians only order when they already suspect the diagnosis) make a model look brilliant in development and useless in practice. -
Prefer simple when simple is as good
A three-question tree that clinicians can read and challenge often beats a slightly more accurate model nobody can explain. When you do use a complex model, check what it's looking at. -
Treat clusters as hypotheses
Clustering always returns groups, and the groups depend on units, starting points and k. They mean something only once they predict outcomes or replicate elsewhere. -
Monitor after launch
Populations, lab assays, devices and practice all drift. Track calibration and alert rates over time, and have a plan for when they slip. -
Report it so others can check
Reporting guidelines like TRIPOD+AI set out what a prediction-model paper should describe. Software that informs diagnosis or treatment may also be regulated as a medical device.