Multiclass Classification
Beyond two buckets
For three-plus classes: one-vs-rest trains one binary model per class, softmax (multi-class logistic) shares a single decision surface, and trees/GBM handle multiclass natively by adding per-class scores.
What you will learn
- Describe OVR vs OVO vs softmax strategies
- Use OneVsRestClassifier / multi_class logistic properly
- Pick multiclass metrics (macro/micro F1, confusion heatmap)
What is this?
Multiclass classification assigns one of K>2 unordered labels. Strategies: One-vs-Rest (K binary classifiers, argmax of their scores/probs), One-vs-One (K(K-1)/2 votes — costlier), softmax multi-class logistic (one weight vector per class, P(class=k|x) = softmax(z_k)), and native multi-output trees. Evaluation: macro-averaged F1 (each class equally) vs micro (pooled counts), plus the K x K confusion matrix.
Why do you need it?
- Products face a menu (retire candidates vs curious vs loyal), not a toggle.
- The encoding strategy changes interpretation and cost; softmax is modern default; OVR is the interop-preserving fallback.
- Macro-vs-micro F1 arguments decide how rare classes count — a metric is a policy.
- Confusion matrices of multiclass models are the finest debugging device in the toolkit.
Mental model
Auditioning K soloists vs teaching one ensemble
OVR vs softmax
OVR: each specialist says 'yes/no for me' and the loudest wins. Softmax: one orchestra line-up picks directly with shared probabilities. Both stage the show; one reuses K binaries, the other learns them together.
How it works
- Label-encode classes first; never leave strings (predict order = label order).
- OVR: fit K binary (e.g., logistic) models; for each row pick argmax class probability.
- Softmax logistic: single model, K output scores, normalize to posterior over classes.
- Trees/forests/GBM: native multiclass via per-class probabilities without strategy wrapping.
- Evaluate: macro F1 (class fairness), micro F1 (heavy-class fairness), and the confusion heatmap.
Two multiclass routes.
Examples
OVR vs softmax on letters
import numpy as npfrom sklearn.datasets import load_digitsfrom sklearn.linear_model import LogisticRegressionfrom sklearn.multiclass import OneVsRestClassifierfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import f1_score, confusion_matrix d = load_digits()Xtr, Xte, ytr, yte = train_test_split(d.data, d.target, test_size=0.3, stratify=d.target, random_state=1) ovr = OneVsRestClassifier(LogisticRegression(max_iter=5000)).fit(Xtr, ytr)sm = LogisticRegression(max_iter=5000, multi_class="multinomial").fit(Xtr, ytr) for name, m in [("one-vs-rest", ovr), ("softmax", sm)]: p = m.predict(Xte) print(f"{name:>11}: macro-F1 {f1_score(yte, p, average='macro'):.4f}" f" accuracy {np.mean(p == yte):.4f}") cm = confusion_matrix(yte, sm.predict(Xte))print("confusion heatmap diagonal:", np.diag(cm).sum(), "/", cm.sum(), "-> classes 3,8,9 hardest:", (np.diag(cm) / np.maximum(cm.sum(axis=1), 1))[3:10].round(2))Both strategies land near-equal scores on digits; the confusion matrix then pinpoints which class pairs the boundary still mixes.
In production
- Document ROI classifier: softmax over 'renew/upsell/ignore' with SHAP for the leading class.
- Incident triage: OVR as model family compatibility when each severity needs its own guardrail.
- Image classes: softmax-based nets output interpretable posteriors directly.
Best practices
- Stratify by class in splits; pick macro vs micro from the stakeholder policy BEFORE modeling; with highly imbalanced multiclass, apply per-class weights and report recall per class; log the label-order mapping.
Common mistakes
- One-vs-all with an unbalanced per-class prior (weights fix); measuring only accuracy where a win on 9-of-10 classes hides a lost class; using strings directly as y in metrics; forgetting stratify produces empty-class folds.
Debugging tips
- Confusion matrix off-diagonal peaks reveal confusable pairs — usually gendered feature veering.
- Aggregate per-class recall; a single 0.x class flags an imbalance-and-metric loophole.
- Check that pipeline labels at predict match training label order (encoder consistency).
Interview checkpoint
Quick quiz
One-vs-Rest trains:
Macro F1 weights classes: