Sub topic 8
Multilabel problem transformation methods
This chapter assumes you know logistic regression, train/test splits, and how a multilabel target is stored as a multi-hot matrix with one column per label. It covers three ways to turn a multilabel problem into problems that ordinary classifiers can solve.
Why transform the problem
Most classifiers in scikit-learn predict one output. A multilabel target has K binary columns. A transformation method rewrites the K-column problem as one or more single-output problems, trains standard models, and joins their outputs. The three common methods are binary relevance, classifier chains, and label powerset.
Binary relevance
Binary relevance trains one independent binary classifier per label. Each model sees the same features and predicts whether its label is present. This is the one-vs-rest setup from the previous chapter, applied to a multi-hot target.
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 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)
br = OneVsRestClassifier(LogisticRegression(max_iter=1000)).fit(X_train, Y_train)
print("binary relevance micro F1:",
round(f1_score(Y_test, br.predict(X_test), average="micro", zero_division=0), 3))Binary relevance is simple and scales linearly with K. Its weakness is that it ignores label dependence. If "beach" and "sea" usually appear together, each model has to discover that from the features alone.
Classifier chains
A classifier chain orders the labels and trains one classifier per label in that order. Classifier i receives the original features plus the labels before it in the chain. During training it uses the true values of those labels. At prediction time it uses the earlier predictions.
from sklearn.multioutput import ClassifierChain
chain = ClassifierChain(
LogisticRegression(max_iter=1000), order="random", random_state=0
).fit(X_train, Y_train)
print("chain micro F1:",
round(f1_score(Y_test, chain.predict(X_test), average="micro", zero_division=0), 3))Chains can capture dependence, but the order matters. An early mistake propagates to later classifiers. A common remedy is to average several chains with different random orders.
Label powerset
Label powerset treats each distinct combination of labels as one class. If the training data contains the combinations {A}, {A, B}, and {B, C}, you get three classes and can train an ordinary multiclass model. Scikit-learn has no dedicated class for this, but you can build it by mapping each row to a combination ID.
import numpy as np
combos, y_power = np.unique(Y_train, axis=0, return_inverse=True)
y_power = y_power.ravel()
lp = LogisticRegression(max_iter=1000).fit(X_train, y_power)
Y_pred = combos[lp.predict(X_test)]
print("powerset micro F1:",
round(f1_score(Y_test, Y_pred, average="micro", zero_division=0), 3))Powerset models label dependence exactly. The cost is a class count that can grow up to 2^K. Many combinations then have few examples, and the model can never predict a combination it did not see in training.
Choosing a method
| Method | Models trained | Uses label dependence | Main risk |
|---|---|---|---|
| Binary relevance | K | No | Inconsistent label sets |
| Classifier chain | K | Yes, in chain order | Order sensitivity, error propagation |
| Label powerset | 1 | Fully | Many rare classes, unseen combinations |
Start with binary relevance as a baseline. Try chains if labels clearly co-occur. Use powerset when K is small and the number of distinct combinations is manageable.
Exercise
A dataset has 20 labels, and the training data contains 4,000 distinct label combinations across 6,000 rows. Why is label powerset a poor fit here, and which method would you try first?
Show answer
Powerset would create 4,000 classes for 6,000 rows, so most classes have one or two examples, and the model cannot predict any combination missing from training. Try binary relevance first as a baseline with 20 models, then a classifier chain if labels co-occur.