All cheat sheets
Part 3 cheat sheet: machine learning
How a model learns, how to measure it, and the main ideas behind the algorithms. Print this sheet
1 Loss and training
Loss is one number for how wrong the model is. Training tries to make it smaller. Gradient descent nudges the knobs (parameters) downhill a little at a time. The learning rate is the size of each step: too big overshoots, too small crawls.
Learn more: How a model learns: loss, training and overfitting
Three bowl-shaped curves, each showing a ball that starts high on the left and takes steps towards the bottom. With a learning rate that is too big, each step overshoots and lands higher on the other side, so the loss grows. With a good rate the steps get smaller and reach the bottom. With a rate that is too small the steps are tiny and the ball crawls.
Too big
Each jump overshoots; the loss grows.
loss (height) against one knob (across)
Just right
Smaller and smaller steps to the bottom.
loss (height) against one knob (across)
Too small
Safe, but slow: it crawls.
loss (height) against one knob (across)
A made-up bowl, loss = knob squared, with the real gradient-descent step. Big dot = start. Learning rates 1.05, 0.3 and 0.05.
Training is a ball rolling down a bowl of loss. The learning rate sets the size of each step: too big overshoots, too small crawls.
2 Underfitting and overfitting
Underfitting: too simple, weak on training and new data. Overfitting: memorises noise, great on training data, worse on new data. Watch training error and validation error together.
Learn more: How a model learns: loss, training and overfitting · Bias, variance and the limits of learning
Three panels with the same ten dots. In the first a straight line misses the curved pattern: underfitting. In the second a smooth curve follows the pattern: a good fit. In the third a wiggly line passes through every dot and chases the noise: overfitting.
Underfitting
Too simple for the pattern:
weak on old and new data.
Good fit
Captures the real pattern:
good on new data too.
Overfitting
Chases the noise in the points:
great on old data, worse on new.
The same 10 made-up points in every panel. The line or curve is the model.
Same data, three models. The straight line is too simple, the wiggly line copies the noise, and the smooth curve is the one that works on new data.
3 Bias and variance
Bias: the model is too simple and misses the pattern. Variance: too sensitive to its training sample. Noise cannot be reduced. More flexible lowers bias and raises variance.
Learn more: Bias, variance and the limits of learning
Four dartboards. Top left: low bias and low variance, darts tightly grouped on the bullseye. Top right: low bias and high variance, darts scattered around the bullseye. Bottom left: high bias and low variance, darts tightly grouped away from the bullseye. Bottom right: high bias and high variance, darts scattered and away from the bullseye.
Low bias
low variance
On target
and consistent.
Low bias
high variance
On target on average
but scattered.
High bias
low variance
Consistent
but off target.
High bias
high variance
Off target and scattered
The bullseye is the true pattern; each dart is a model trained on a different sample of the data.
Bias is how far the average dart is from the bullseye; variance is how scattered the darts are. Each dart is a model trained on a different sample.
4 Measuring honestly
Accuracy misleads when one class is rare. Use a confusion matrix, then precision, recall and F1. Cross-validation averages several splits so one lucky split cannot fool you. Keep the test set out of all tuning.
Learn more: Measuring models honestly · Classification: picking a category
Two rows of small shapes for the fraud example. The first row shows the 20 real frauds: 15 solid squares for frauds caught and 5 dashed squares for frauds missed. The second row shows the 45 payments the model flagged: the same 15 solid squares for real frauds and 30 diamonds for false alarms. Three cards give the results: recall 15 divided by 20 is 0.75, precision 15 divided by 45 is about 0.33, and accuracy 965 divided by 1,000 is 0.965.
Real frauds: 20 of the 1,000 payments
15 caught
5 missed
Flagged by the model: 45 payments
15 real frauds
30 false alarms
Recall
15 ÷ 20 = 0.75
Of the real frauds,
how many were caught.
Precision
15 ÷ 45 ≈ 0.33
Of the flagged payments,
how many were real.
Accuracy
965 ÷ 1,000 = 0.965
Looks high, yet 5 frauds
were missed.
Solid square = fraud caught. Dashed square = fraud missed. Diamond = false alarm. Numbers from the run in this lesson.
Out of 1,000 payments, 20 are fraud. The model flags 45 of them: 15 real frauds and 30 false alarms, and it misses 5 frauds. Recall asks "how much fraud did we catch?", precision asks "how many flags were real?", and accuracy hides both.
5 Regression errors
MAE: the average size of a mistake, in the units of the target. RMSE: like MAE but punishes big mistakes more. R squared: how much of the ups and downs the model explains (1 is best).
Learn more: Regression: predicting a number
A scatter of ten dots with a fitted line. Vertical orange segments from each dot to the line show the errors, called residuals. Text explains that MAE is the average gap, RMSE punishes big gaps more, and R squared says how much of the ups and downs the model explains.
Regression error
Dots = real values.
Line = the model's guesses.
Orange gaps = the errors
(residuals).
MAE: the average gap.
RMSE: punishes big gaps
more.
R²: how much of the
ups and downs it explains.
Made-up points. Smaller MAE and RMSE are better; R² is better the closer it is to 1.
The error measures are all based on the gaps between the real values and the model's line. The points are made up.
6 Which algorithm
Start with logistic or linear regression as a baseline. On tables of data, random forests and gradient boosting usually win. Neural networks win on images, audio and free text. Naive Bayes is a very fast text baseline.
Learn more: The main ML algorithms and when to use each
7 Trees, forests, boosting
A decision tree asks yes/no questions; it is easy to explain but overfits. A random forest averages many trees trained on random re-samples. Boosting builds trees one after another, each fixing the last one's mistakes.
Learn more: The main ML algorithms and when to use each · More ensemble methods, AdaBoost by hand
Three small decision trees, each trained on a different random re-sample. Trees 1 and 3 vote yes and tree 2 votes no. The forest answer is yes, two votes to one.
Each tree sees a different random re-sample of the data
Tree 1
question
votes yes
Tree 2
question
votes no
Tree 3
question
votes yes
Forest answer: yes (2 to 1)
Many trees,
one vote.
Made-up example with three trees. A real forest has hundreds.
Averaging many different trees steadies a model that one tree overfits.
A random forest trains many different trees and lets them vote. The three trees here are a made-up example.
8 Learning without labels
k-means: assign points to the nearest centre, move the centres, repeat. PCA: describe the data by its few strongest directions. Anomaly detection learns what normal looks like and flags what is far from it.
Learn more: Unsupervised learning: groups, compression and anomalies
Three panels with the same ten points and two diamond-shaped centres. Panel 1: each point joins its nearest centre. Panel 2: each centre moves to the middle of its points. Panel 3: repeating until the groups stop changing.
1. Pick centres, assign
each point joins its nearest centre
2. Move the centres
to the middle of their points
3. Repeat until still
the groups stop changing
k-means with k = 2 (diamonds are the centres)
Made-up points. The method needs no labels: it finds the groups from distances alone.
k-means assigns each point to its nearest centre, moves the centres to the middle of their points, and repeats. The points are made up.
9 Probability and cause
Bayes' rule updates a belief with evidence; rare things stay rare even after a good test. Correlation is not causation: a hidden factor can drive two things.
Learn more: Thinking in probabilities · Cause and effect, an introduction to causal inference
Two horizontal bars. The first splits 10,000 people into 100 who are ill and 9,900 who are healthy. The second zooms in on the 585 people with a positive test: 90 are really ill and 495 are healthy people with a false alarm. Text below says 90 divided by 585 is about 15 percent, not 90 percent.
10,000 people
100 ill (1 in 100)
9,900 healthy
Only those with a positive test: 585 people (zoomed in)
90 really ill
495 healthy people with a false alarm
90 of 100 ill people test positive. 5% of the 9,900 healthy people also do: 495.
A positive test means: 90 ÷ (90 + 495) = 90 ÷ 585
which is about 15% chance of being ill, not 90%.
Made-up numbers, as in the lesson's maths box. The bars are drawn to scale in each row.
Counting people shows why a positive test on a rare illness is usually a false alarm: most positives come from the large healthy group.