Classification metrics: accuracy, precision, recall, F1
Accuracy is only the start. Learn what precision, recall and F1 measure, how to compute them from counts, and how to choose by the cost of each kind of error.
Lesson 35 of 6610 minBeginner
You will learn to
- Compute accuracy, precision, recall and F1
- Pick a metric from the cost of each error
- Read macro and weighted averages
Before this lesson
Intuition
Accuracy answers one question: "What share of predictions were right?" It is easy to understand and often misleading. If 99 of 100 customers do not churn, a model that always says "will not churn" scores 99 percent and is useless.
The fix is to ask separate questions about the two kinds of mistake. Imagine a smoke alarm. It can fail in two ways: it rings when there is no fire (a false alarm) or it stays silent during a fire (a miss). Precision asks: "When the alarm rings, how often is there really a fire?" Recall asks: "Of all real fires, how many did the alarm catch?" A very nervous alarm has high recall and low precision. A very cautious one has the opposite. F1 is a single number that is high only when both are reasonably high.
Which one you care about depends on what each mistake costs. Missing a fraud costs money; blocking a good customer costs goodwill. You choose the metric from that trade-off, not from habit.
Picture it
All of these metrics are built from four counts: true positives (TP), false negatives (FN), false positives (FP) and true negatives (TN). The table of those counts is the confusion matrix. Rows are the true class, columns the predicted class.
The math
With the positive class being the one you want to find:
F1 is the harmonic mean of precision and recall. A harmonic mean is pulled toward the smaller value, so you cannot get a high F1 by excelling at one and ignoring the other.
For more than two classes, compute the metric for each class (treating it as "positive" and all others as "negative") and then average:
- Macro average: the plain mean over classes. Every class counts equally, so a weak small class is visible.
- Weighted average: the mean weighted by how many true rows each class has. It follows overall performance but can hide a small class.
Worked example
A spam filter checks 200 emails, 40 of them spam. It flags 35 emails: 30 are spam, 5 are not. So TP = 30, FP = 5, FN = 40 - 30 = 10 and TN = 200 - 30 - 5 - 10 = 155.
- Accuracy = (30 + 155) / 200 = 0.925.
- Precision = 30 / 35 = 0.857: when it says spam, it is right 86 percent of the time.
- Recall = 30 / 40 = 0.75: it catches three quarters of the spam.
- F1 = 2 × 30 / (60 + 5 + 10) = 60 / 75 = 0.80.
Compare with a lazy filter that never flags anything: accuracy = 160 / 200 = 0.80, recall = 0. Accuracy alone makes the real filter look only a little better.
Now score the "not spam" class the same way. Its precision is 155 / 165 = 0.939, recall 155 / 160 = 0.969 and F1 = 310 / 325 = 0.954. The macro F1 is (0.80 + 0.954) / 2 = 0.877. The weighted F1 is (40 × 0.80 + 160 × 0.954) / 200 = 0.923. The weighted value is higher because the easy majority class has four times the weight.
How mlflow does it
For classification runs, the leaderboard shows Accuracy, AUC, Recall, Prec., F1, Kappa and MCC. With two classes, Recall, Prec. and F1 score the positive class (the second class in sorted order). With more than two classes they are weighted averages, and AUC is computed one class against the rest and weighted. In all of them, higher is better. Click a column header to rank by it. See the metrics reference for the definitions.
Kappa and MCC are single-number summaries that correct for chance agreement and stay informative when classes are unbalanced.
Common mistakes
- Trusting accuracy on imbalanced data. The majority class hides failure on the rare class. Look at recall and precision for the class you care about, or at F1.
- Optimizing F1 when the costs are not equal. F1 treats precision and recall as equally important. Decide which error is costlier and rank by that metric, or move the threshold (see the precision-recall lesson).
- Reading weighted averages as per-class quality. A weak small class disappears in them. Check macro averages and the confusion matrix too.
- Not knowing which class is positive. Precision and recall of the other class are different numbers. Check which class a score refers to.
- Comparing scores from different data splits. A test set with a different class mix changes precision. Compare models on the same folds.
Try it in mlflow
Sort a classification leaderboard
- Open Runs and open a finished classification run.
- On the leaderboard, sort by Accuracy and note the top model.
- Now sort by Recall, then by F1. Does the top model change?
- If it changes, ask which kind of error is more expensive for your problem and rank by that metric.