Sub topic 11
Evaluating multilabel models
This chapter assumes you know binary precision, recall and F1, can read a confusion matrix, and have seen micro, macro and weighted averaging. It also assumes you can fit a one-vs-rest model in scikit-learn.
Why multilabel needs its own metrics
In a multilabel problem each sample has a set of true labels and a set of predicted labels. A prediction can be fully right, partly right, or fully wrong. Plain accuracy treats every sample as either correct or incorrect, so it hides partial credit. The metrics below measure the result at different levels: the whole label set, each label slot, or each sample.
The main metrics
| Metric | What it counts | Notes |
|---|---|---|
| Subset accuracy | Share of samples whose whole label set matches exactly | Strictest. Drops fast as the label count grows. |
| Hamming loss | Share of individual sample-label cells that are wrong | Lower is better. Forgives partial errors. |
| Micro F1 | Pools true positives, false positives and false negatives over all labels | Dominated by frequent labels. |
| Macro F1 | Averages per-label F1 with equal weight | Exposes weak rare labels. |
| Samples F1 | Computes F1 per sample, then averages | Matches the per-item view of a result. |
Hamming loss can look good when most labels are absent. If each sample has 2 of 5 labels, predicting nothing is wrong in only 40% of cells. Always read Hamming loss next to F1.
Running the metrics
This code trains one logistic regression per label, then scores the same probabilities at two thresholds.
from sklearn.datasets import make_multilabel_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import accuracy_score, hamming_loss, f1_score
X, Y = make_multilabel_classification(
n_samples=1000, n_features=20, n_classes=5, n_labels=2, random_state=0
)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, random_state=0)
# Binary relevance: one independent logistic regression per label.
model = OneVsRestClassifier(LogisticRegression(max_iter=1000)).fit(X_train, Y_train)
proba = model.predict_proba(X_test) # shape (n_samples, n_labels)
for threshold in (0.3, 0.5):
Y_pred = (proba >= threshold).astype(int)
print("threshold", threshold)
print(" subset accuracy:", round(accuracy_score(Y_test, Y_pred), 3))
print(" Hamming loss: ", round(hamming_loss(Y_test, Y_pred), 3))
print(" micro F1: ", round(f1_score(Y_test, Y_pred, average="micro", zero_division=0), 3))
print(" macro F1: ", round(f1_score(Y_test, Y_pred, average="macro", zero_division=0), 3))In scikit-learn, accuracy_score on a multilabel indicator matrix is subset accuracy. Lowering the threshold usually raises recall and lowers precision, so the F1 scores and Hamming loss move in ways you should check, not assume.
Reading the numbers together
Use these patterns when you compare results:
- Micro F1 well above macro F1 means frequent labels do fine and rare labels do poorly.
- Low subset accuracy with good Hamming loss means errors are spread thinly across many samples.
- A threshold change that raises micro F1 but lowers macro F1 may be helping common labels at the cost of rare ones.
Common errors
- Reporting only subset accuracy. It is often close to zero for large label sets, even for a useful model.
- Reporting only Hamming loss on sparse labels. A trivial model can score well.
- Scoring hard predictions when you need ranking quality. Metrics like average precision work on probabilities and don't depend on one threshold.
- Evaluating on a split where rare labels are missing from the test set, which makes per-label scores undefined or misleading.
Exercise
A model scores micro F1 of 0.82 and macro F1 of 0.51 on a multilabel task. What does the gap suggest, and which metric would you check next?
Show answer
Frequent labels are predicted well while rare labels are predicted poorly, because micro F1 is dominated by frequent labels and macro F1 weights every label equally. Check per-label F1 (for example with f1_score(..., average=None)) and the label counts to find which rare labels fail, then consider per-label thresholds or imbalance handling.