Evaluating Classifiers: Precision, Recall, F1 and ROC
Build a confusion matrix, compute precision, recall and F1, pick a decision threshold that matches real costs, and read ROC curves and AUC.
Welcome back!
Step 1 of 12Learn
Accuracy is not enough
A team builds a model to detect a manufacturing defect that occurs in 2% of parts. Their model has 98% accuracy. Then someone notices it predicts "no defect" for every single part.
With imbalanced classes, accuracy rewards ignoring the rare class. To see what a classifier actually does, start with the confusion matrix, counting four outcomes on the test set:
| Actual positive | Actual negative | |
|---|---|---|
| Predicted positive | TP | FP |
| Predicted negative | FN | TN |
From it come the key metrics:
- Accuracy = (TP + TN) ÷ total
- Precision = TP ÷ (TP + FP): of the items flagged, how many were right?
- Recall (sensitivity, true positive rate) = TP ÷ (TP + FN): of the real positives, how many were found?
- Specificity = TN ÷ (TN + FP): of the real negatives, how many were correctly cleared?
- F1 score = 2 × precision × recall ÷ (precision + recall)
The F1 score is the harmonic mean of precision and recall. Unlike an ordinary average, it is dragged down sharply by whichever is lower. A model with precision 1.0 and recall 0.1 has an arithmetic mean of 0.55 but an F1 of only about 0.18. F1 is useful when you want a single number that rewards balance, especially with imbalanced classes.
Worked example: TP = 40, FP = 10, FN = 20, TN = 930.
- Precision = 40 ÷ 50 = 0.80
- Recall = 40 ÷ 60 ≈ 0.667
- F1 = 2 × 0.80 × 0.667 ÷ (0.80 + 0.667) ≈ 0.727
- Accuracy = 970 ÷ 1,000 = 0.97
Step 2 of 12Practice · Diagram
The confusion matrix
Every prediction lands in one of four boxes. Tap each.
The model passes a defective part as fine. Which box is that?
Tap the shape that answers the question, then press Check.
- False negative (FN)
A real positive the model missed. Lowers recall.
- True positive (TP)
A real positive the model caught.
- True negative (TN)
A real negative correctly left alone.
- False positive (FP)
A false alarm. Lowers precision.
Step 3 of 12Try the calculator
Classifier metrics
Enter confusion-matrix counts. Watch how accuracy barely moves while precision, recall and F1 change a lot when the positive class is rare.
- Accuracy
- 0.97
- Precision
- 0.8
- Recall
- 0.667
- Specificity
- 0.989
- F1 score
- 0.727
Step 4 of 12Practice · Code challenge
Write f1(tp, fp, fn) that returns the F1 score rounded to 3 decimal places. If there are no true positives, return 0. (Tip: F1 also equals 2·TP ÷ (2·TP + FP + FN).)
f1(8, 2, 2)f1(5, 5, 0)f1(0, 3, 3)f1(40, 10, 20)
Step 5 of 12Learn
Thresholds and trade-offs
Most classifiers output a score or probability, not a hard yes or no. The threshold you choose to turn scores into decisions is a separate, important choice, and 0.5 is only a default.
Lowering the threshold flags more items as positive:
- Recall rises: you catch more real positives.
- Precision falls: more of the flags are false alarms.
Raising the threshold does the reverse.
The right threshold depends on the costs of each error, which is a business or ethical question, not a purely technical one:
- Cancer screening: a missed cancer (FN) is far worse than a false alarm (FP) that leads to a follow-up test. Use a low threshold for high recall.
- Spam filtering: losing a real email (FP) is worse than seeing an extra spam message (FN). Use a higher threshold for high precision.
- Fraud detection: both errors cost money: missed fraud loses the transaction amount, while false alarms annoy customers and cost review time. You can choose the threshold that minimizes expected total cost.
Example: each missed fraud costs $500 and each false alarm costs $5 in review time and customer friction. Threshold A gives 30 misses and 400 false alarms: cost = 30 × 500 + 400 × 5 = $17,000. Threshold B gives 15 misses and 1,500 false alarms: cost = 15 × 500 + 1,500 × 5 = $15,000. Threshold B is cheaper despite producing almost four times as many alerts.
Always choose the threshold on validation data, never on the test set, and report precision and recall at the chosen threshold.
Step 6 of 12Practice · Chart
Moving the threshold trades recall for precision
For cancer screening, where misses are far worse than false alarms, tap a sensible threshold.
Tap the point that answers the question, then press Check.
| Threshold | Recall | Precision |
|---|---|---|
| 0.1 | 0.98 | 0.2 |
| 0.2 | 0.95 | 0.3 |
| 0.3 | 0.9 | 0.42 |
| 0.4 | 0.84 | 0.53 |
| 0.5 | 0.75 | 0.64 |
| 0.6 | 0.63 | 0.74 |
| 0.7 | 0.5 | 0.83 |
| 0.8 | 0.35 | 0.9 |
| 0.9 | 0.18 | 0.95 |
Step 7 of 12Practice · Work it out
Missed defects cost $200 each and false alarms cost $10 each. Threshold X produces 12 missed defects and 150 false alarms. What is its total error cost in dollars?
Step 8 of 12Learn
ROC curves and AUC
To compare classifiers independently of any single threshold, practitioners use the ROC curve (receiver operating characteristic, a name inherited from radar engineering).
For every possible threshold, compute two rates:
- True positive rate (TPR) = recall = TP ÷ (TP + FN)
- False positive rate (FPR) = FP ÷ (FP + TN) = 1 − specificity
Plot TPR (vertical) against FPR (horizontal). A very high threshold flags nothing: the point (0, 0). A very low threshold flags everything: the point (1, 1). The curve traces everything in between.
- A perfect classifier shoots straight up to TPR = 1 at FPR = 0, hugging the top-left corner.
- A random classifier follows the diagonal from (0, 0) to (1, 1).
The area under the curve (AUC) summarizes the whole curve in one number between 0 and 1:
- AUC = 0.5: no better than random guessing.
- AUC = 1.0: perfect separation.
- AUC has a neat interpretation: it is the probability that the model gives a randomly chosen positive example a higher score than a randomly chosen negative one.
AUC is useful for comparing models' ranking ability. But it has limits. With highly imbalanced data, ROC curves can look optimistic, because FPR stays tiny even when false positives outnumber true positives. In those cases, the precision-recall curve, and the area under it, often gives a more honest picture. And a high AUC doesn't tell you which threshold to use; that still depends on costs.
Step 9 of 12Chart
Two ROC curves and the random diagonal
| False positive rate | Model A | Model B | Random guessing |
|---|---|---|---|
| FPR 0 | 0 | 0 | 0 |
| 0.1 | 0.6 | 0.3 | 0.1 |
| 0.2 | 0.78 | 0.45 | 0.2 |
| 0.4 | 0.9 | 0.65 | 0.4 |
| 0.6 | 0.95 | 0.8 | 0.6 |
| 0.8 | 0.98 | 0.92 | 0.8 |
| 1.0 | 1 | 1 | 1 |
Step 10 of 12Practice · Match the pairs
Match each metric to the question it answers.
Tap an answer, then tap the place where it belongs (or drag it there).
- Precision
- Recall
- Specificity
- F1 score
- AUC
Step 11 of 12Practice · What would you do?
What would you do?
You are building a classifier to flag possibly fraudulent insurance claims for human investigators. Investigators can review about 50 claims a day, and most claims are honest. Which evaluation approach fits best?
Step 12 of 12Practice · Choose one
Model A has precision 0.95 and recall 0.20. Model B has precision 0.70 and recall 0.65. Which has the higher F1 score?
Lesson complete!