Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 10: Metrics: Accuracy, Precision, Recall and F1

A single number can hide a multitude of sins, and in classification no number hides more than accuracy. Accuracy is simply the fraction of predictions the model got right, and it sounds like the perfect measure — until the data is uneven. Imagine a worked example: a screening test for a rare condition that affects 1 person in 100. A model that always answers no will be right 99 times out of 100, scoring 99% accuracy while finding nobody who is ill. The number looks superb and the model is useless. Accuracy is only honest when the classes are roughly balanced; everywhere else, you need better measures.

The better measures start from four counts. For a yes-or-no task, every prediction falls into one of four boxes: true positives, where the model said yes and was right; true negatives, where it said no and was right; false positives, where it said yes and was wrong; and false negatives, where it said no and was wrong. Those four boxes, laid out as a table, are called a confusion matrix, and almost every classification measure is built from them. Accuracy itself is just the correct boxes divided by all the boxes.

Precision and recall: two different kinds of right

Precision answers the question: of all the cases the model flagged as yes, how many really were yes? High precision means few false alarms. Recall answers the opposite question: of all the cases that really were yes, how many did the model find? High recall means few missed cases. The two pull against each other, because lowering the threshold flags more cases — raising recall but lowering precision — while raising it does the reverse. Which matters more depends on the cost of each mistake. In the screening test, a missed case is far worse than a false alarm, so recall matters most. In a spam filter, a genuine email sent to spam is the painful mistake, so precision matters most.

Because choosing between precision and recall is awkward, people often combine them into a single number called the F1 score, which is the harmonic mean of the two. The harmonic mean has a useful property: it is dragged down by whichever of the two is lower, so a model cannot score well by being excellent at one and terrible at the other. An F1 of 0.9 means both precision and recall are high; an F1 of 0.4 means at least one of them is poor. It is a fair single summary when both kinds of mistake matter, though it hides the trade-off itself, so professionals still look at precision and recall separately before deciding.

One more measure: ROC and AUC

Precision, recall and F1 all depend on the threshold you choose, which raises a question: how good is the model regardless of threshold? The answer is a curve. Imagine moving the threshold from one end to the other and plotting, at each step, how many real yes cases the model finds against how many false alarms it raises. That curve is called the ROC curve, and the area under it — the AUC — is a single number summarising the model across all thresholds. An AUC of 1.0 is a perfect model; 0.5 is no better than guessing. The AUC is especially useful when you have not yet decided which mistake matters more, because it measures the model's ability to rank cases — to put the real yes cases ahead of the no cases — before any threshold is chosen. Like every measure, it has limits: it can look good even when the model is useless for a rare class, which is why it should be reported alongside precision and recall, not instead of them.

The lesson of this chapter is a habit, not a formula: always ask what a metric hides before you trust it. A model can have high accuracy and miss every case that matters, high precision and find almost nothing, or high recall and cry wolf all day. The right metric follows from the real-world cost of each kind of mistake, and that cost is decided by the people affected, not by the maths. When you evaluate your own capstone model in Lesson 20, you will choose your metrics this way — by asking which mistake would hurt most.

Key Terms

Accuracy
The fraction of all predictions a model got right; misleading when the classes are unevenly sized.
Confusion matrix
A table of four counts — true positives, true negatives, false positives and false negatives — that summarises a classifier's mistakes.
True positive
A case the model correctly flagged as yes.
False positive
A case the model wrongly flagged as yes, such as a genuine email sent to spam.
False negative
A case the model wrongly called no, such as an ill patient the screening test missed.
Precision
Of all the cases the model flagged as yes, the fraction that really were yes; high precision means few false alarms.
Recall
Of all the cases that really were yes, the fraction the model found; high recall means few missed cases.
F1 score
A single number combining precision and recall, dragged down by whichever is lower, so both must be good to score well.

Discussion Questions

  1. A spam filter has 99% accuracy but sends important emails to spam. Which measure would expose the problem, and why?
  2. A fire alarm that never misses a fire but goes off every time someone makes toast: high precision or high recall? Is it a good alarm?
  3. When would you care more about precision than recall, and when the reverse? Give an example of each.

Practice Questions

  1. Why can 99% accuracy be a useless result?
  2. What is a confusion matrix?
  3. What is the difference between precision and recall?
  4. A screening test misses ill patients. Which measure should you watch, and why?
  5. Why is the F1 score a fair single summary?
Show answers
  1. If the classes are uneven, a model can score 99% by always predicting the common class. A model that always says no to a condition affecting 1 in 100 people scores 99% while finding nobody who is ill.
  2. A table of true positives, true negatives, false positives and false negatives. It shows exactly which kinds of mistakes a classifier makes, which accuracy alone hides.
  3. Precision is how many flagged cases were really yes; recall is how many real yes cases were found. Precision punishes false alarms, recall punishes missed cases.
  4. Recall, because it measures how many real cases the model found. Missing a case is the costly mistake here, so the share of real cases found matters most.
  5. Because it is dragged down by whichever of precision and recall is lower. A model cannot score well on F1 by being excellent at one and terrible at the other; both must be good.
Keep reading to complete this lesson