A model can be 99% accurate and completely useless. To see how, and to judge any model that sorts things into yes or no, you need four numbers arranged in a little square called the confusion matrix. It’s simply a count of the ways a model can be right and wrong, and it’s much less confusing than its name.
From a score to a decision#
Take a spam filter. For every email it produces a score between 0 and 1: how spammy the email looks. A score isn’t a decision yet. Something has to turn it into “spam” or “fine”, and that something is a threshold: flag every email that scores above it.
Below are 40 emails, placed by the score a filter gave them. The top row is real spam, the bottom row is fine. Everything right of the line gets flagged.
The four boxes#
Every email ends up in exactly one of four boxes, depending on what it really was and what the filter said:
- True positive (TP): spam, and flagged. A catch.
- False negative (FN): spam, but let through. A miss.
- False positive (FP): fine, but flagged. A false alarm.
- True negative (TN): fine, and left alone.
“Positive” just means the thing you’re looking for (spam, fraud, a disease). The first word, true or false, says whether the model was right. That’s the whole matrix: two ways to be right, and two very different ways to be wrong.
Precision and recall#
Two questions about those boxes matter more than any others:
- Precision: when the model says spam, how often is it right? TP ÷ (TP + FP). Low precision means your inbox loses real emails.
- Recall: of all the real spam, how much did it catch? TP ÷ (TP + FN). Low recall means spam gets through.
Predict first
In the figure, move the threshold from 0.5 up to 0.8. What happens?
That trade-off is the most important thing in this tutorial. You can almost never have both for free. You choose where the line goes, and where it should go depends on which mistake costs more.
Which mistake can you live with?#
Predict first
A model screens scans for an early sign of cancer, and a doctor checks everything it flags. Which mistake is worse?
Predict first
People complain that your spam filter keeps hiding real emails from their bank and their boss. Which number is too low?
The 99% accuracy trap#
Accuracy, the share of all calls that were right, sounds like the obvious score. It hides exactly the thing you care about whenever the thing you’re looking for is rare.
Predict first
A fraud model checks 1,000 card payments, 10 of them fraud, and scores 99% accuracy. Is it any good?
The real model is less accurate than doing nothing, at 95.8%, and it’s the only one of the three worth using. When one outcome is rare, ignore accuracy and look at the matrix, or at precision and recall.
In code#
scikit-learn computes all of this for you. Here, eight emails and the scores a model gave them:
from sklearn.metrics import confusion_matrix, precision_score, recall_score
y_true = [1, 0, 1, 1, 0, 0, 1, 0] # 1 = really spam
scores = [0.9, 0.4, 0.65, 0.3, 0.2, 0.7, 0.8, 0.1] # the model's spam scores
threshold = 0.5
y_pred = [1 if s >= threshold else 0 for s in scores]
print(confusion_matrix(y_true, y_pred)) # [[3 1]
# [1 3]]
print(precision_score(y_true, y_pred)) # 0.75
print(recall_score(y_true, y_pred)) # 0.75One catch: scikit-learn prints the boxes in a different order from the figure above. Its rows are the true label and its columns the prediction, with the negative class first, so the layout is [[TN, FP], [FN, TP]]. Always check which way round a matrix is before you read it.
What to remember
- A threshold turns a model’s scores into decisions, and every decision lands in one of four boxes: TP, FN, FP or TN.
- Precision asks “when it flags, is it right?”; recall asks “did it catch them all?”. Moving the threshold trades one for the other.
- Choose the threshold by which mistake costs more, and never trust accuracy alone when the thing you’re looking for is rare.