Sub topic 10
Micro, macro and weighted averages
This chapter assumes you know precision, recall and F1 for a binary problem, and that you have read a confusion matrix. With K classes you get K sets of those numbers. Averaging turns them into one score. The averaging method you pick changes what that score tells you.
Per-class scores first
Treat each class as "this class vs. everything else" and count true positives (TP), false positives (FP) and false negatives (FN) for it. Each class then has its own precision, recall and F1. The three averages differ only in how they combine those counts.
The three averages
- Macro computes the metric for each class, then takes the plain mean. Every class counts equally, so rare classes matter as much as common ones.
- Weighted is also a mean of per-class metrics, but each class is weighted by its support (the number of true samples of that class).
- Micro adds up TP, FP and FN over all classes first, then computes the metric once. Every sample decision counts equally, so large classes dominate.
A worked example
Ten samples: six of class 0, two of class 1 and two of class 2. The model favors the big class.
from sklearn.metrics import confusion_matrix, f1_score
y_true = [0] * 6 + [1] * 2 + [2] * 2
y_pred = [0, 0, 0, 0, 0, 1, 1, 0, 2, 0]
print(confusion_matrix(y_true, y_pred))
for avg in ("micro", "macro", "weighted"):
print(avg, round(f1_score(y_true, y_pred, average=avg), 3))The confusion matrix is [[5 1 0] [1 1 0] [1 0 1]]. Per-class F1 is 0.769 for class 0, 0.5 for class 1 and 0.667 for class 2.
| Average | How it is computed | F1 |
|---|---|---|
| Micro | 2·7 / (2·7 + 3 + 3) | 0.700 |
| Macro | (0.769 + 0.5 + 0.667) / 3 | 0.645 |
| Weighted | (6·0.769 + 2·0.5 + 2·0.667) / 10 | 0.695 |
Macro is lowest because the two weak small classes pull it down. Weighted and micro stay close to the score on class 0.
Choosing an average
- Use macro when every class matters equally, such as rare-disease detection. It exposes failures on small classes.
- Use weighted when you want a per-class view that still reflects the real class mix.
- Use micro when each individual decision matters equally. In multilabel problems it pools every sample-label pair.
Common errors
- Calling
f1_scoreon multiclass data withoutaverage=. The default is"binary", which raises an error for more than two classes. - Comparing a macro score from one run with a weighted score from another.
- Forgetting
zero_division=0when a class is never predicted. Its precision is undefined, and scikit-learn warns and substitutes 0.
Exercise
In the example above, class 2 has precision 1.0 but F1 of only 0.667. Why does macro F1 still drop, and which average would hide this problem most?
Show answer
Class 2 has recall 0.5, because one of its two samples was predicted as class 0. F1 combines precision and recall, so the low recall pulls it to 0.667. Macro gives class 2 a full one-third of the weight, so the loss shows. Micro hides it most, because class 2 has only 2 of the 10 samples and micro weights every sample equally.