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. It also assumes you know that scikit-learn puts actual labels on rows and predicted labels on columns. The question here is how to turn one number per class into one number for the whole model.
Why one number per class is not enough
A three-class matrix gives you three precision values, three recall values and three F1 values. Model comparisons, dashboards and hyperparameter searches usually need a single score. Averaging is how you get one, but there are several ways to do it, and they can give very different answers on the same predictions.
The three standard options are:
- Macro: compute the metric for each class, then take the plain mean.
- Weighted: compute the metric for each class, then take the mean weighted by class size.
- Micro: pool the TP, FP and FN counts of all classes, then compute the metric once.
Support is the number of actual examples of a class. It is the row sum of the matrix, and the weighted average uses it.
Per-class counts from a multiclass matrix
Every class is treated one-vs-rest. For class k:
- TP is the diagonal cell for k.
- FP is the rest of column k, meaning other classes predicted as k.
- FN is the rest of row k, meaning k examples predicted as something else.
The code below uses the animal matrix from the previous chapter and computes everything by hand with NumPy. It then checks the result against scikit-learn.
import numpy as np
from sklearn.metrics import confusion_matrix, precision_recall_fscore_support
labels = ["bird", "cat", "dog"]
y_true = ["cat", "cat", "cat", "dog", "dog", "dog", "bird", "bird", "bird", "bird"]
y_pred = ["cat", "cat", "dog", "dog", "dog", "dog", "bird", "cat", "bird", "bird"]
cm = confusion_matrix(y_true, y_pred, labels=labels)
tp = np.diag(cm)
precision = tp / cm.sum(axis=0) # column sums = predicted counts
recall = tp / cm.sum(axis=1) # row sums = actual counts (support)
support = cm.sum(axis=1)
f1 = 2 * precision * recall / (precision + recall)
print(precision.round(3)) # [1. 0.667 0.75 ]
print(recall.round(3)) # [0.75 0.667 1. ]
print(f1.round(3)) # [0.857 0.667 0.857]
print(support) # [4 3 3]
for average in ("macro", "micro", "weighted"):
p, r, f, _ = precision_recall_fscore_support(
y_true, y_pred, labels=labels, average=average
)
print(average, round(p, 3), round(r, 3), round(f, 3))
# macro 0.806 0.806 0.794
# micro 0.8 0.8 0.8
# weighted 0.825 0.8 0.8The next sections show where each of those averaged numbers comes from.
Macro average
The macro average treats every class as equally important.
- Macro precision is (1.000 + 0.667 + 0.750) / 3 ≈ 0.806.
- Macro recall is (0.750 + 0.667 + 1.000) / 3 ≈ 0.806.
- Macro F1 is (0.857 + 0.667 + 0.857) / 3 ≈ 0.794.
Notice that macro F1 (0.794) is not the harmonic mean of macro precision and macro recall (which would be 0.806 here). scikit-learn averages the per-class F1 scores. It does not compute F1 from the averaged precision and recall. Some other libraries do it the other way, so check the documentation when you compare numbers across tools.
Use macro when rare classes matter as much as common ones. A model that fails completely on one class loses a full share of the score. This makes macro the harshest of the three averages on imbalanced data.
Micro average
The micro average sums the one-vs-rest counts over all classes and then applies the formula once.
- Total TP is 3 + 2 + 3 = 8.
- Every misclassified example is a false positive for the class it was predicted as. The same example is a false negative for its actual class. Total FP and total FN are therefore both 2, the number of errors.
- Micro precision is 8 / (8 + 2) = 0.8.
- Micro recall is also 8 / (8 + 2) = 0.8.
Micro F1 is therefore 0.8 as well. For single-label multiclass problems, where each example has exactly one true class and one predicted class, micro precision, micro recall, micro F1 and accuracy are all the same number. Reporting all four adds no information.
Micro averaging weights every example equally, so large classes dominate. It is useful when you care about overall hit rate across examples. It is also the usual choice in multilabel problems, where accuracy is not defined the same way.
Weighted average
The weighted average computes the per-class metric and then weights each class by its support. Here the weights are 4/10, 3/10 and 3/10.
- Weighted precision is 0.4 × 1.000 + 0.3 × 0.667 + 0.3 × 0.750 = 0.825.
- Weighted recall is 0.4 × 0.750 + 0.3 × 0.667 + 0.3 × 1.000 = 0.800.
- Weighted F1 is 0.4 × 0.857 + 0.3 × 0.667 + 0.3 × 0.857 = 0.800.
Weighted recall always equals accuracy. This is because each class's recall times its support is its TP count, and the sum of those is the total number of correct predictions.
Weighted precision and F1 do not collapse into accuracy in general. The F1 matches 0.8 in this example by coincidence of the numbers.
The weighted average sits between macro and micro. It still reports per-class precision and F1, but it lets large classes count for more.
Comparing the three on imbalanced data
The differences become large when class sizes are far apart. Below, a model always predicts the common class on a 90/10 split.
from sklearn.metrics import precision_recall_fscore_support
y_true = ["common"] * 90 + ["rare"] * 10
y_pred = ["common"] * 100
for average in ("macro", "micro", "weighted"):
p, r, f, _ = precision_recall_fscore_support(
y_true, y_pred, labels=["common", "rare"],
average=average, zero_division=0,
)
print(average, round(p, 3), round(r, 3), round(f, 3))
# macro 0.45 0.5 0.474
# micro 0.9 0.9 0.9
# weighted 0.81 0.9 0.853The model never finds a single rare example, yet micro reports 0.9 and weighted recall reports 0.9. Macro recall is 0.5, which exposes the failure. This is the accuracy trap from earlier in the tutorial, now showing up in the averages. Micro and weighted recall inherit it because both are dominated by the large class.
The zero_division=0 argument matters here. The rare class has no predicted positives, so its precision is 0/0. By default scikit-learn warns and substitutes 0. Setting the argument explicitly makes the choice visible. Pick the value deliberately, because it changes the macro score.
Choosing an average
| Average | Weights each | Equals accuracy? | Best when |
|---|---|---|---|
| Macro | class equally | No | Every class matters, including rare ones |
| Micro | example equally | Yes (single-label) | You care about overall hit rate |
| Weighted | class by support | Recall only | You want per-class detail, scaled to real class frequencies |
Common errors
- Reporting only one average. On imbalanced data, print macro and micro together. A large gap between them signals poor performance on small classes.
- Averaging F1 from averaged precision and recall. Know which convention your tool uses. scikit-learn averages per-class F1.
- Using
average="binary"on multiclass data. The default forprecision_scoreand similar functions is"binary". It raises an error when there are more than two labels. Choose an average explicitly. - Forgetting
labels. If a class never appears iny_trueory_pred, leaving it out oflabelschanges the macro average. If you include it, its support is zero and its scores are undefined, sozero_divisionapplies. - Treating weighted as a fix for imbalance. It reflects the imbalance and hides rare-class failure in the same way accuracy does.
- Mixing the average with the matrix orientation. Per-class recall uses row sums and per-class precision uses column sums in scikit-learn's layout. A transposed matrix swaps the two and also swaps what the macro scores mean.
Exercises
Exercise
A three-class matrix has rows (actual) and columns (predicted) in the order A, B, C: [[8, 1, 1], [2, 3, 0], [0, 0, 5]]. Compute macro recall, micro recall and weighted recall.
Show answer
Per-class recall is A 8/10 = 0.8, B 3/5 = 0.6, C 5/5 = 1.0. Macro recall is (0.8 + 0.6 + 1.0) / 3 = 0.8. Micro recall is total TP / total examples = 16 / 20 = 0.8. Weighted recall uses supports 10, 5, 5, giving (10 × 0.8 + 5 × 0.6 + 5 × 1.0) / 20 = 16 / 20 = 0.8. All three equal 0.8 here by coincidence of the numbers. Weighted and micro recall must always match, but macro usually differs.
Exercise
A fraud model is evaluated on 980 legitimate and 20 fraudulent transactions. It labels everything legitimate. What are macro recall and weighted recall, and which would you report to a stakeholder who wants to know whether fraud is caught?
Show answer
Recall for the legitimate class is 980/980 = 1.0 and for the fraud class is 0/20 = 0.0. Macro recall is (1.0 + 0.0) / 2 = 0.5. Weighted recall is (980 × 1.0 + 20 × 0.0) / 1000 = 0.98. Report macro recall, and ideally the fraud-class recall by itself, because weighted recall equals accuracy and hides that no fraud is caught.
Exercise
In a single-label multiclass problem, why are micro precision and micro recall always equal?
Show answer
Every wrong prediction is both a false positive (for the predicted class) and a false negative (for the actual class). Summed over all classes, total FP equals total FN. Since total TP is shared, TP / (TP + FP) and TP / (TP + FN) are the same value, which is also accuracy.