Sub topic 10
Micro, macro and weighted averages
This chapter assumes you know precision, recall and F1 for a binary problem, can read a confusion matrix, and can run scikit-learn code in Python. Here you learn how to collapse per-class scores into one number, and why the choice of average changes what the number means.
Why averaging is needed
A binary problem has one precision, one recall and one F1. A problem with K classes has K of each, because every class can be treated as "positive" against all the others. To report a single score, you must average. The three standard ways are micro, macro and weighted. They answer different questions, and on imbalanced data they can disagree widely.
Every method starts from the same per-class counts, taken one-vs-rest:
- TP (true positives): samples of class c predicted as c.
- FP (false positives): samples of other classes predicted as c.
- FN (false negatives): samples of class c predicted as something else.
The three averages
Macro average
Macro computes the metric for each class, then takes the plain mean. Every class has the same weight, whatever its size.
macro_F1 = (F1_1 + F1_2 + ... + F1_K) / KUse macro when rare classes matter as much as common ones. A model that ignores a rare class is punished hard.
Weighted average
Weighted also computes the metric per class, then averages with weights equal to each class's support (its number of true samples).
weighted_F1 = sum(support_c * F1_c) / sum(support_c)It reflects performance on the data as it is distributed. Rare classes contribute little.
Micro average
Micro pools the counts over all classes first, then computes the metric once.
micro_precision = sum(TP_c) / (sum(TP_c) + sum(FP_c))
micro_recall = sum(TP_c) / (sum(TP_c) + sum(FN_c))Large classes dominate because they contribute most of the counts.
| Average | Computed how | Who dominates | Typical use |
|---|---|---|---|
| Macro | Mean of per-class scores | All classes equally | Rare classes matter |
| Weighted | Support-weighted mean of per-class scores | Frequent classes | Report overall quality on the real distribution |
| Micro | Metric from pooled counts | Frequent classes | Overall rate of correct decisions |
A worked example
Consider three classes with very different sizes. The counts below come from a confusion matrix over 100 samples.
| Class | Support | TP | FP | FN | Precision | Recall | F1 |
|---|---|---|---|---|---|---|---|
| A | 80 | 76 | 6 | 4 | 0.927 | 0.950 | 0.938 |
| B | 15 | 9 | 2 | 6 | 0.818 | 0.600 | 0.692 |
| C | 5 | 1 | 2 | 4 | 0.333 | 0.200 | 0.250 |
Macro F1 is (0.938 + 0.692 + 0.250) / 3 = 0.627. The weak class C pulls it down.
Weighted F1 is (80 × 0.938 + 15 × 0.692 + 5 × 0.250) / 100 = 0.867. Class A dominates.
For micro, the pooled counts are TP = 86, FP = 10, FN = 14. Micro precision is 86 / 96 = 0.896 and micro recall is 86 / 100 = 0.860. Micro F1 is about 0.878.
The same predictions produce 0.627, 0.867 and 0.878. None is wrong. Each measures something different, so always state which one you report.
A property of micro averaging in multiclass
In single-label multiclass, every sample has exactly one true class and one predicted class. Each wrong prediction is a false positive for one class and a false negative for another. So the summed FP equals the summed FN, and micro precision, micro recall and micro F1 are all equal to accuracy. In the example, all three are 0.86 if you use the pooled counts correctly with FP = FN = 14. (The table above has FP summing to 10 because it is a simplified illustration. In a real confusion matrix the two sums always match.)
This is why scikit-learn's classification_report shows a single "accuracy" row for multiclass problems instead of a micro-average row. In multilabel problems the equality does not hold, because a sample can have several true and several predicted labels. There, micro average appears as its own row.
Computing the averages in scikit-learn
The average argument selects the method. Passing average=None returns the per-class scores so you can see what is being averaged.
import numpy as np
from sklearn.metrics import f1_score, precision_score, recall_score
# 100 samples: class 0 is common, class 2 is rare.
y_true = np.array([0] * 80 + [1] * 15 + [2] * 5)
y_pred = np.array(
[0] * 76 + [1] * 3 + [2] * 1 # true 0: 76 right, 3 -> 1, 1 -> 2
+ [1] * 9 + [0] * 4 + [2] * 2 # true 1: 9 right, 4 -> 0, 2 -> 2
+ [2] * 1 + [0] * 3 + [1] * 1 # true 2: 1 right, 3 -> 0, 1 -> 1
)
print("per-class F1:", f1_score(y_true, y_pred, average=None).round(3))
for avg in ("micro", "macro", "weighted"):
print(
avg,
"precision", round(precision_score(y_true, y_pred, average=avg), 3),
"recall", round(recall_score(y_true, y_pred, average=avg), 3),
"F1", round(f1_score(y_true, y_pred, average=avg), 3),
)
print("accuracy:", (y_true == y_pred).mean())Run it and compare. Micro precision, recall and F1 are identical and match accuracy, while macro sits lower because class 2 is weak.
Averaging in the multilabel case
For multilabel targets, the y arrays are indicator matrices with one column per label. The same three options apply, plus one more:
average="micro"pools TP, FP and FN over all label columns.average="macro"averages the per-label scores equally.average="weighted"weights each label by how many true positives it has.average="samples"computes the metric for each sample across its labels, then averages over samples.
Micro and macro behave as above. Sample-wise F1 asks how well each individual row was labeled, which suits applications where each item is judged on its whole label set.
Common errors
- Reporting one average without naming it. "F1 = 0.88" is ambiguous. Write "micro F1" or "macro F1".
- Using micro F1 to check rare-class performance. Micro hides a class that is almost never right. Check macro F1 or the per-class report.
- Using macro F1 when a rare class is not important. One tiny, noisy class can drag macro down and swamp real progress elsewhere.
- Treating weighted F1 as a fairness metric. It is close to accuracy-like behavior on imbalanced data. It can look good while a minority class fails.
- Ignoring undefined values. If a class is never predicted, its precision is 0/0. scikit-learn warns and sets it to 0 by default. Pass
zero_division=0(or 1) to make the choice explicit, and remember that this choice shifts the macro average. - Comparing scores across datasets with different class balance. Micro and weighted values move with the class distribution, even if the model is unchanged.
Choosing an average
- Ask first which mistakes cost the most. If every class matters equally, use macro.
- If you care about overall volume of correct decisions on real traffic, use micro (or accuracy in single-label multiclass).
- If you want a per-class view that still reflects the real mix, use weighted, and read it next to the per-class report.
- On imbalanced problems, report macro and micro together. A large gap between them signals that rare classes are doing worse than frequent ones.
Exercises
Exercise
A three-class model has per-class F1 scores of 0.90, 0.80 and 0.10, with supports of 700, 250 and 50. Compute the macro F1 and the weighted F1, and say which one better exposes the weak class.
Show answer
Macro F1 = (0.90 + 0.80 + 0.10) / 3 = 0.60. Weighted F1 = (700 × 0.90 + 250 × 0.80 + 50 × 0.10) / 1000 = (630 + 200 + 5) / 1000 = 0.835. Macro exposes the weak class, because each class counts equally. Weighted is dominated by the 700-sample class.
Exercise
In a single-label multiclass problem, a model makes 30 errors on 200 test samples. What are its micro precision, micro recall and micro F1, and why are they equal?
Show answer
All three equal accuracy, which is 170 / 200 = 0.85. Each error is one false positive for the predicted class and one false negative for the true class, so total FP = total FN = 30. With equal FP and FN, micro precision and micro recall are the same, and so is their harmonic mean.
Exercise
A multilabel model has a micro F1 of 0.82 and a macro F1 of 0.48. What does the gap suggest, and what is your next step?
Show answer
The pooled counts are dominated by frequent labels that the model handles well, while several rare labels score poorly and pull macro down. Print per-label scores with average=None, find the weak labels, and try per-label thresholds or imbalance handling for them.