Sub topic 11
Multiclass problems: macro, micro, and weighted averages
This chapter assumes you can read a binary confusion matrix and compute precision, recall, and F1 from TP, FP, and FN. It also assumes you have used scikit-learn's metric functions. Here you extend those metrics to three or more classes.
From one score to many
Precision and recall are defined for a single positive class. With three classes there is no single "positive" class. The standard approach is one-vs-rest: for each class in turn, treat it as positive and treat all other classes as negative. That gives one precision, recall, and F1 per class. An averaging rule then combines them into a single number.
A worked example
Take three classes, 0, 1, and 2, with support (the number of true examples) of 6, 3, and 1.
y_true = [0, 0, 0, 0, 0, 0, 1, 1, 1, 2]
y_pred = [0, 0, 0, 0, 0, 1, 1, 1, 0, 1]The confusion matrix has true classes as rows and predicted classes as columns:
| True \ Predicted | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 5 | 1 | 0 |
| 1 | 1 | 2 | 0 |
| 2 | 0 | 1 | 0 |
For each class, TP is the diagonal cell. FP is the rest of its column. FN is the rest of its row.
| Class | TP | FP | FN | Precision | Recall | F1 |
|---|---|---|---|---|---|---|
| 0 | 5 | 1 | 1 | 0.83 | 0.83 | 0.83 |
| 1 | 2 | 2 | 1 | 0.50 | 0.67 | 0.57 |
| 2 | 0 | 0 | 1 | undefined | 0.00 | 0.00 |
Class 2 is never predicted, so its precision is 0/0. scikit-learn warns and substitutes a value. You choose that value with zero_division.
The three averaging rules
Macro averages the per-class scores with equal weight. Macro recall is (0.83 + 0.67 + 0) / 3 = 0.50. The rare class 2 counts as much as the common class 0, so macro scores drop when a small class is handled badly.
Weighted also averages per-class scores, but each class is weighted by its support. Weighted recall is (6 × 0.83 + 3 × 0.67 + 1 × 0) / 10 = 0.70. Large classes dominate, so the failure on class 2 matters less.
Micro pools the counts across classes first, then computes the metric once. Total TP is 7, total FP is 3, and total FN is 3. Micro precision is 7 / 10 = 0.70, and micro recall is also 0.70. Every error is a false positive for one class and a false negative for another, so these totals are always equal. In single-label multiclass problems, micro precision, micro recall, micro F1, and accuracy are therefore the same number.
from sklearn.metrics import precision_recall_fscore_support
y_true = [0, 0, 0, 0, 0, 0, 1, 1, 1, 2]
y_pred = [0, 0, 0, 0, 0, 1, 1, 1, 0, 1]
for avg in ("macro", "micro", "weighted"):
p, r, f, _ = precision_recall_fscore_support(
y_true, y_pred, average=avg, zero_division=0
)
print(avg, round(p, 2), round(r, 2), round(f, 2))
# macro 0.44 0.5 0.47
# micro 0.7 0.7 0.7
# weighted 0.65 0.7 0.67The macro precision of 0.44 is (0.83 + 0.50 + 0) / 3. It counts class 2 as 0 because of zero_division=0. With zero_division=1, class 2 would count as a perfect 1.0, and macro precision would rise to 0.78. That would be misleading, because the model never found that class.
To see the per-class numbers behind an average, pass average=None. You get one value per class, in sorted label order.
Choosing an average
| Average | Treats classes as | Use when |
|---|---|---|
| macro | equally important | rare classes matter as much as common ones |
| weighted | important in proportion to size | you want a summary that reflects the real label mix |
| micro | pooled examples | overall correctness matters (it equals accuracy here) |
If you report only micro or weighted scores on imbalanced data, a failure on a rare class can hide inside a healthy-looking number. Macro F1 and the per-class table together show it.
Common errors
- Reporting an average without naming it. "F1 = 0.67" could mean macro, micro, or weighted, and the values often differ.
- Leaving
zero_divisionat its default and not noticing the warning. A class that is never predicted can silently change the average. - Forgetting that micro-averaged scores equal accuracy for single-label problems, so reporting both adds no information.
- Reading the confusion matrix with rows and columns swapped. In scikit-learn, rows are true labels and columns are predictions.
Exercise
A model on a three-class problem has per-class recalls of 0.90, 0.60, and 0.30, with supports of 50, 30, and 20. Compute macro recall and weighted recall. Which one is lower, and what does the gap tell you?
Show answer
Macro recall is (0.90 + 0.60 + 0.30) / 3 = 0.60. Weighted recall is (50 × 0.90 + 30 × 0.60 + 20 × 0.30) / 100 = (45 + 18 + 6) / 100 = 0.69. Macro is lower because the weakest class (0.30) is also the smallest, and macro gives it the same weight as the strongest class. The gap shows the model performs worse on the smaller classes.
Exercise
A multiclass model makes 12 mistakes on 100 test examples. What are its micro-averaged precision and recall, and why?
Show answer
Both are 0.88. Each of the 12 mistakes is one false positive for the predicted class and one false negative for the true class, so total FP = total FN = 12. Total TP is 88. Micro precision = 88 / (88 + 12) = 0.88, and micro recall = 88 / (88 + 12) = 0.88. This equals the accuracy.