Sub topic 14
Label dependence and co-occurrence
This chapter assumes you know binary relevance (one independent classifier per label), multi-hot target matrices, and micro and macro F1. It builds on the multilabel transformation methods from earlier chapters.
What label dependence means
Labels are dependent when knowing one label changes the probability of another. A photo tagged "beach" is more likely to be tagged "sea" than a random photo is. A news article tagged "football" is almost never tagged "cooking".
Binary relevance trains each label on its own and ignores this. Each model sees only the features X. It never uses what the other labels say. If the features carry enough signal, this loses little. If the features are noisy or weak, label relationships can supply information that the features lack.
Measuring co-occurrence
Start by looking at the data. For a multi-hot matrix Y with shape (n_samples, n_labels), the product Y.T @ Y counts how often each pair of labels appears together. The diagonal holds each label's frequency. Dividing a row by its diagonal entry gives the conditional probability P(label j | label i).
import numpy as np
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.multioutput import ClassifierChain
from sklearn.metrics import accuracy_score, 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)
co = Y_train.T @ Y_train
cond = co / np.diag(co)[:, None] # row i, column j = P(label j | label i)
print("co-occurrence counts:\n", co)
print("conditional probabilities:\n", cond.round(2))
br = OneVsRestClassifier(LogisticRegression(max_iter=1000)).fit(X_train, Y_train)
chain = ClassifierChain(
LogisticRegression(max_iter=1000), order="random", random_state=0
).fit(X_train, Y_train)
for name, model in (("binary relevance", br), ("classifier chain", chain)):
pred = model.predict(X_test)
print(name,
"subset accuracy", round(accuracy_score(Y_test, pred), 3),
"micro F1", round(f1_score(Y_test, pred, average="micro", zero_division=0), 3))Compare each conditional probability with the label's overall frequency, np.diag(co) / len(Y_train). Pairs where P(j | i) is far from the base rate of j are the dependent pairs.
How a classifier chain uses dependence
A classifier chain orders the labels. The classifier for label k receives X plus the values of the earlier labels in the chain. During training it uses the true earlier labels. At prediction time it uses the earlier predictions.
Common errors and trade-offs
- Order matters. A chain with a different label order gives different models. Averaging several chains with random orders (an ensemble) reduces this sensitivity.
- Error propagation. A wrong early prediction becomes a wrong input to later classifiers.
- Dependence is not always useful. If binary relevance already scores well, a chain may add cost without gain. Always compare against the baseline on held-out data.
- Metric choice. Dependence helps most with subset accuracy, which rewards getting the whole label set right. Micro F1 may barely change.
- Rare combinations. The label powerset method captures dependence fully by treating each distinct label set as one class. It cannot predict combinations unseen in training.
Exercise
In the co-occurrence matrix co, label A has a diagonal count of 200 and the A-B entry is 150. Label B appears in 300 of 1000 samples. Is B dependent on A, and what is P(B | A)?
Show answer
P(B | A) = 150 / 200 = 0.75, while B's base rate is 300 / 1000 = 0.30. The probability more than doubles when A is present, so the labels are dependent. A classifier chain could exploit this.