Accuracy, Precision and Recall Explained
When people say a model is "very accurate", it sounds like the end of the story. It is not. A model can be highly accurate and completely useless. To see why, and to know which number to look at instead, you need one small table: the confusion matrix.
The confusion matrix
Take a model that answers a yes-or-no question, such as "is this email spam?". Call the thing you are looking for (spam) the positive class and the other (real mail) the negative class. "Positive" just means "the thing we are looking for", not "good".
Run the model on a labeled test set and every example lands in one of four boxes:
| Model says positive | Model says negative | |
|---|---|---|
| Really positive | True positive (TP): found it | False negative (FN): missed it |
| Really negative | False positive (FP): false alarm | True negative (TN): correctly ignored |
"True" and "false" say whether the model was right. "Positive" and "negative" say what the model answered. So a false positive is a wrong "yes", and a false negative is a wrong "no".
The five numbers
- Accuracy = (TP + TN) / total. Of everything, how much did it get right?
- Precision = TP / (TP + FP). Of the ones it flagged, how many were right?
- Recall = TP / (TP + FN). Of the real ones, how many did it catch? (Also called sensitivity.)
- Specificity = TN / (TN + FP). Of the real negatives, how many did it correctly leave alone?
- F1 = 2 x precision x recall / (precision + recall). A single number that is only high when precision and recall are both high. It can also be worked out straight from the counts as 2TP / (2TP + FP + FN).
Example 1: an ordinary model
Here is a made-up test set of 200 examples (this is also the example the calculator starts with):
| Says positive | Says negative | |
|---|---|---|
| Really positive (110) | TP = 80 | FN = 30 |
| Really negative (90) | FP = 20 | TN = 70 |
- Accuracy = (80 + 70) / 200 = 0.75
- Precision = 80 / (80 + 20) = 0.8
- Recall = 80 / (80 + 30) = 0.7273
- Specificity = 70 / (70 + 20) = 0.7778
- F1 = 2 x 80 / (160 + 20 + 30) = 0.7619
So: when this model says "positive" it is right 8 times in 10, but it misses about 3 in every 11 real positives.
Example 2: the lazy model
Now a made-up test set of 1,000 examples where only 10 are positive (rare things, like a fault in a machine part). A "model" that always answers "no" gives:
| Says positive | Says negative | |
|---|---|---|
| Really positive (10) | TP = 0 | FN = 10 |
| Really negative (990) | FP = 0 | TN = 990 |
- Accuracy = (0 + 990) / 1,000 = 0.99
- Recall = 0 / 10 = 0
- Specificity = 990 / 990 = 1
- Precision = 0 / 0, which cannot be calculated because it never said "yes". The calculator shows 0 in that case, and F1 is 0 too.
99 percent accurate, and it catches nothing. When one class is rare, accuracy mostly measures how rare it is. That is why you should always compare a model with this kind of lazy baseline (always guess the commonest answer) before calling it good, and look at recall and precision as well as accuracy.
Which number matters for which job
Precision and recall usually pull against each other. A model that flags more things catches more real ones (recall goes up) but raises more false alarms (precision goes down). Which matters more depends on what a mistake costs.
A spam filter that hides real mail
Positive = spam. A false positive is a real email hidden in the spam folder: an invitation or a message from a teacher that you never see. A false negative is one junk email that gets through, which is only annoying. Here the costly mistake is the false positive, so precision matters most: when it says "spam", it had better be right.
A fire alarm that misses a fire
Positive = fire. A false negative is a real fire with no alarm, which is dangerous. A false positive is an alarm for burnt toast: annoying, but safe. Here the costly mistake is the false negative, so recall matters most: catch every real fire, even at the cost of some false alarms.
Other cases
- When both kinds of mistake matter about equally, look at F1, or at precision and recall side by side.
- When wrongly accusing someone is the worst outcome, as with a tool that flags pupils' work as copied, watch precision and specificity closely, because every false positive is an innocent person. The guide to why AI detectors get it wrong works through this with numbers.
- When the classes are balanced and mistakes cost the same, accuracy is a reasonable summary.
Measuring, not predicting
The Model Accuracy Predictor on this site works out accuracy, precision, recall, F1 and specificity from a confusion matrix: you type in the four counts from a model you have already tested, and it does the arithmetic above to 4 decimal places. Despite its name, it cannot tell you how accurate a model will be before you test it. Nothing can. The only way to know is to run the model on a labeled test set it has never seen and count. (If the test set leaked into training, even that count is wrong; see overfitting and underfitting.)
One more check: every group
A single overall score can hide a model that works well for most examples and badly for one group, such as one accent in a voice tool or one kind of leaf in a sorter. Make a confusion matrix for each group separately and compare. The guide to AI bias and fairness shows a worked example where overall accuracy looks fine and one group is let down.
Try it
A made-up plant-disease spotter is tested on 100 leaves: 20 are diseased. It flags 25 leaves, of which 15 are really diseased. Work out TP, FP, FN and TN, then precision and recall. (Answer: TP = 15, FP = 10, FN = 5, TN = 70; precision = 15 / 25 = 0.6; recall = 15 / 20 = 0.75.) Is that a good spotter? It depends what a missed diseased leaf costs compared with a healthy one thrown away.