Sub topic 14
Macro, micro and weighted averages
This chapter assumes you can read a multiclass confusion matrix and compute precision, recall and F1 for one class. Those are the per-class metrics from the earlier chapters.
Why you need an average
A multiclass problem gives you one precision, one recall and one F1 for each class. A report that lists them all is accurate but hard to compare. If you want to rank two models, you need a single number. Macro, micro and weighted averaging are three ways to collapse the per-class values into one. They answer different questions, so they can disagree.
The three methods
- Macro computes the metric for each class, then takes a plain mean. Every class counts equally, whatever its size.
- Micro pools the counts first. It sums TP, FP and FN over all classes, then computes the metric once.
- Weighted takes the mean of the per-class metrics, weighted by each class's support. Support is the number of actual examples of that class.
For single-label multiclass data, every wrong prediction is one false positive for one class and one false negative for another. So micro precision, micro recall, micro F1 and accuracy are all the same number.
Worked example
This uses the bird, cat and dog data from the previous chapter. The per-class values are:
| Class | Support | Precision | Recall | F1 |
|---|---|---|---|---|
| bird | 4 | 1.000 | 0.750 | 0.857 |
| cat | 3 | 0.667 | 0.667 | 0.667 |
| dog | 3 | 0.750 | 1.000 | 0.857 |
from sklearn.metrics import precision_recall_fscore_support
y_true = ["cat", "cat", "cat", "dog", "dog", "dog", "bird", "bird", "bird", "bird"]
y_pred = ["cat", "cat", "dog", "dog", "dog", "dog", "bird", "cat", "bird", "bird"]
for average in ("macro", "micro", "weighted"):
p, r, f, _ = precision_recall_fscore_support(
y_true, y_pred, average=average, zero_division=0
)
print(f"{average:9} precision={p:.3f} recall={r:.3f} f1={f:.3f}")
# macro precision=0.806 recall=0.806 f1=0.794
# micro precision=0.800 recall=0.800 f1=0.800
# weighted precision=0.825 recall=0.800 f1=0.800Macro F1 is the mean of 0.857, 0.667 and 0.857, which is 0.794. Micro is 8 correct out of 10, or 0.800. Weighted precision is (4 × 1.000 + 3 × 0.667 + 3 × 0.750) / 10 = 0.825.
Choosing an average
- Use macro when rare classes matter. A model that ignores a small class gets a low macro score, because that class's recall drags the mean down.
- Use micro when you care about overall correctness per example. It is dominated by the large classes.
- Use weighted when you want per-class metrics but a score that matches how often each class occurs.
Common errors
- Comparing numbers from different averages. Macro F1 of 0.79 and weighted F1 of 0.80 describe different things.
- Expecting macro F1 to equal the F1 of macro precision and macro recall. Macro F1 is the mean of per-class F1 values, so the two usually differ slightly.
- Hiding a failing class. Weighted and micro scores can look good while a small class has recall near zero. Always inspect the per-class rows too.
- Ignoring
zero_division. A class that is never predicted has undefined precision. scikit-learn warns and uses 0 unless you set the parameter.
Exercise
A model is evaluated on 990 "normal" examples and 10 "fault" examples. It finds none of the faults, and every normal example is classified correctly. Which is higher: micro recall or macro recall? Give the approximate values.
Show answer
Micro recall is higher. It equals accuracy, which is 990 / 1000 = 0.99. Macro recall is the mean of 1.0 (normal) and 0.0 (fault), which is 0.5. Macro averaging exposes the failure on the rare class, while micro averaging hides it.