Find a course

Module 3: Classification Models and MetricsLesson 7 of 1115 min

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.

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 positiveActual negative
Predicted positiveTPFP
Predicted negativeFNTN

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.

The confusion matrixTwo-by-two matrix: False negative (FN), True positive (TP), True negative (TN), False positive (FP).False negative (FN)True positive (TP)True negative (TN)False positive (FP)PredictedNegativePositiveActualNegativePositive
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).)

Runs in a sandbox in your browser.
  • 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.

Moving the threshold trades recall for precisionLine chart with 9 categories: 0.1: Recall 0.98, Precision 0.2; 0.2: Recall 0.95, Precision 0.3; 0.3: Recall 0.9, Precision 0.42; 0.4: Recall 0.84, Precision 0.53; 0.5: Recall 0.75, Precision 0.64; 0.6: Recall 0.63, Precision 0.74; 0.7: Recall 0.5, Precision 0.83; 0.8: Recall 0.35, Precision 0.9; 0.9: Recall 0.18, Precision 0.95.00.20.40.60.810.10.20.30.40.50.60.70.80.9Threshold
Illustrative curves for one classifier.
Moving the threshold trades recall for precision
ThresholdRecallPrecision
0.10.980.2
0.20.950.3
0.30.90.42
0.40.840.53
0.50.750.64
0.60.630.74
0.70.50.83
0.80.350.9
0.90.180.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

Two ROC curves and the random diagonalLine chart with 7 categories: FPR 0: Model A 0, Model B 0, Random guessing 0; 0.1: Model A 0.6, Model B 0.3, Random guessing 0.1; 0.2: Model A 0.78, Model B 0.45, Random guessing 0.2; 0.4: Model A 0.9, Model B 0.65, Random guessing 0.4; 0.6: Model A 0.95, Model B 0.8, Random guessing 0.6; 0.8: Model A 0.98, Model B 0.92, Random guessing 0.8; 1.0: Model A 1, Model B 1, Random guessing 1.00.20.40.60.81True positive rateFPR 00.10.20.40.60.81.0False positive rate
Illustrative curves. Model A hugs the top-left corner, so it has the larger AUC.
Two ROC curves and the random diagonal
False positive rateModel AModel BRandom guessing
FPR 0000
0.10.60.30.1
0.20.780.450.2
0.40.90.650.4
0.60.950.80.6
0.80.980.920.8
1.0111

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).

  1. Precision
  2. Recall
  3. Specificity
  4. F1 score
  5. 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?