Sub topic 3
Micro, Macro and Weighted Averages
Assume you know binary precision, recall, F1, train/test splits, and logistic regression. This chapter extends those ideas to multiple classes and labels.
Choosing an average
For several classes or labels, calculate a metric separately, then combine the results.
- Micro averaging adds all true positives, false positives, and false negatives before calculating the metric. It emphasizes common classes.
- Macro averaging calculates one metric per class or label, then takes their unweighted mean. Every class counts equally.
- Weighted averaging takes the mean weighted by each class's support, which is its number of true examples.
| Average | Useful when | Main risk |
|---|---|---|
| Micro | Overall performance matters | Rare labels can disappear |
| Macro | Every class matters equally | Small classes can make the score noisy |
| Weighted | Class frequencies reflect importance | Common classes dominate |
Evaluating multilabel models
In multilabel classification, one example can have several correct labels.
- Hamming loss is the fraction of individual label decisions that are wrong.
- Subset accuracy is exact-match accuracy: every label for an example must be correct.
- Sample-wise F1 computes F1 for each example's label set.
- Micro and macro F1 summarize label-level performance using the averaging rules above.
Subset accuracy is strict. Hamming loss can look good even when the model misses an important rare label.
Tuning thresholds per label
A sigmoid probability is not automatically a final prediction. A global threshold of 0.5 is only a starting point. Choose a separate threshold for each label on validation data, using the metric that matches the application.
from sklearn.datasets import make_multilabel_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
from sklearn.model_selection import train_test_split
from sklearn.multiclass import OneVsRestClassifier
X, Y = make_multilabel_classification(
n_samples=800, n_features=20, n_classes=4, random_state=0
)
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, random_state=0)
model = OneVsRestClassifier(LogisticRegression(max_iter=1000)).fit(X_train, Y_train)
P = model.predict_proba(X_val)
thresholds = []
for j in range(Y.shape[1]):
choices = [0.2, 0.4, 0.5, 0.6, 0.8]
thresholds.append(max(choices, key=lambda t: f1_score(
Y_val[:, j], P[:, j] >= t, zero_division=0)))
print(thresholds)Imbalance and label dependence
Class or label imbalance can hide poor rare-class recall. Use macro metrics, class weights, resampling, and a decision threshold chosen for the rare class. In multilabel data, labels may co-occur. Binary relevance models each label independently, so classifier chains can help when one label predicts another.
Validation and calibration
Use stratification for multiclass splits so class proportions remain similar. For multilabel data, ordinary stratification cannot preserve every label combination reliably; use iterative-stratified splitting when available. Calibration measures whether predicted probabilities match observed frequencies. Calibrated probabilities make threshold selection more meaningful, but calibration must use separate validation data.
> callout note > Tune thresholds and calibration on validation data, then evaluate once on untouched test data.
Exercise
Why might macro F1 be much lower than micro F1?
Show answer
A rare class or label performs poorly. Macro F1 gives it equal weight, while micro F1 is dominated by common decisions.