Multiclass & Multilabel Classification
Go beyond yes/no questions — One-vs-Rest, One-vs-One, softmax regression, metric averaging for many classes, and models that assign several labels at once.
So far every classifier we've built answered a yes/no question: malignant or benign, fraud or legit. Real problems are rarely that tidy — an iris flower is one of three species, a handwritten digit is one of ten, and a movie can be an action comedy and a romance all at once. This lesson extends everything you know about binary classification to many classes, and then to many labels per sample.
One question, how many answers?
Three different problem shapes hide behind the word "classification":
- Binary — exactly one of 2 classes. Is this tumor malignant?
- Multiclass — exactly one of K classes. Which iris species is this: setosa, versicolor, or virginica? The classes are mutually exclusive — a flower can't be two species.
- Multilabel — any subset of K labels. Which genres describe this
movie?
["action", "comedy"]is a perfectly valid answer, and so is an empty set or all of them.
The distinction matters because it changes the model's output, the loss, and the metrics. Confuse multiclass with multilabel and you'll force a movie to have exactly one genre — or let a flower be two species at once.
Turning binary classifiers into multiclass ones
Logistic regression natively separates two classes. Two classic strategies recycle it for K classes:
One-vs-Rest (OvR) trains K binary classifiers, each answering "is it class k, or anything else?" — setosa vs rest, versicolor vs rest, virginica vs rest. At prediction time, all K models score the sample and the most confident one wins. K models total, each trained on the full dataset.
One-vs-One (OvO) trains one binary classifier per pair of classes —
K·(K−1)/2 models — and lets them vote. That's more models, but each trains on
only two classes' worth of data, which is a win for algorithms whose training
cost grows steeply with dataset size (scikit-learn's SVC uses OvO internally
for exactly this reason).
Softmax: the multiclass sigmoid
There's a more elegant third option. Instead of gluing binary models together, generalize the sigmoid itself. Softmax takes K raw scores and turns them into K probabilities that sum to 1: exponentiate every score, then divide each by the total.
A logistic regression trained this way — one weight vector per class, softmax
on top, cross-entropy loss — is called multinomial (softmax) logistic
regression. It's one coherent model rather than K independent ones, so the
probabilities are directly comparable. Modern scikit-learn does this
automatically: hand LogisticRegression a target with three classes and it
fits the multinomial model, no extra arguments needed.
Multiclass in practice: iris
Everything else about the workflow is unchanged — same pipeline, same fit,
same predict_proba (now with three columns). The confusion matrix just grows
to K×K:
The matrix tells a story accuracy can't: setosa is never confused with anything (its petals are unmistakably small), while the mistakes live entirely in the versicolor/virginica corner — those two species genuinely overlap.
Macro, micro, weighted: averaging metrics across classes
Precision, recall, and F1 are defined per class — with K classes you get K of each. To report a single number you must average them, and how you average changes the answer:
- macro — average the K per-class scores equally. Every class counts the same, so a tiny minority class can drag the score down. Use it when small classes matter.
- micro — pool all predictions first, then compute the metric once. Big classes dominate. (For single-label multiclass, micro-F1 equals plain accuracy.)
- weighted — macro, but each class weighted by its number of samples. A compromise that still mostly reflects the majority.
On balanced iris these barely differ. On imbalanced data they diverge wildly:
The model nails the 70% majority class and largely misses the 10% minority. Micro-F1 (= accuracy here) looks respectable; macro-F1 exposes the failure. When someone reports "the F1", always ask which averaging.
Multilabel: many tags per sample
Now drop the "exactly one" constraint. A multilabel target is a binary
matrix: one column per label, one row per sample, with 1s wherever a label
applies. MultiLabelBinarizer converts lists of tags into that matrix, and
OneVsRestClassifier fits one independent binary classifier per column —
each label gets its own yes/no decision, so a sample can light up several:
Two multilabel-specific metrics appear here. Hamming loss is the fraction of individual label decisions that were wrong — every cell of the matrix counts, so 0.09 means 9% of all yes/no calls missed. The exact-match rate (subset accuracy) is far stricter: a sample only counts if every label is right. It's normal for hamming loss to look great while exact match looks mediocre — one wrong tag out of four ruins the exact match but barely moves the hamming loss.
Which problem do you actually have?
Ask: can the true answers overlap? Species, digit, sentiment — no overlap → multiclass, use softmax. Genres, article topics, symptoms — overlaps are meaningful → multilabel, use one binary decision per label.
Check your understanding
Q1.Tagging news articles with topics (politics, sports, tech — possibly several per article) is which kind of problem?
Q2.For a 10-class problem, how many binary models do One-vs-Rest and One-vs-One train, respectively?
Q3.What does the softmax function guarantee about its outputs?
Q4.A 3-class dataset is 90% class A. Your model predicts class A perfectly but fails on B and C. Which F1 average will look worst?
Q5.A multilabel model has hamming loss 0.05 but exact-match rate 0.60. What does this mean?
Exercise: Find the hardest genre
Using the synthetic movie-tag dataset from this lesson, compute the per-label F1 score (one number per genre) and each label's frequency in the data. Which genre is hardest to predict — and looking at how the tags were generated, can you explain why that label is fundamentally harder than the others?
Next module: a classifier with a completely different philosophy — support vector machines, which ignore most of the data and let a handful of boundary points decide everything.