Week 7 · Multiclass classifiers
Read the words of the video
- Imagine a machine that only says 'yes' or 'no'. But what if a post office needs to read a '3' or a '7'? Or a zoo app must identify a specific animal from many? Sometimes, our computers need to choose one answer from many options, not just two. This is where clever tricks help them do more!
- To choose from many, we can use two main tricks. In 'one-versus-rest', we make a 'yes or no' machine for each choice, like 'is it a three?' or 'is it a seven?'. Then, we pick the answer that gets the strongest 'yes'. The other trick, 'one-versus-one', pairs up every choice against each other.
- Let's try an example. If we have four types of animals – say, cats, dogs, birds, and fish – the 'one-versus-rest' trick needs four yes-or-no machines, one for each animal. But for 'one-versus-one', we need to compare every pair. That means six separate yes-or-no machines in total! See the difference?
- For exams, remember that 'one-versus-one' usually needs more yes-or-no machines than 'one-versus-rest', especially for many choices! We also learned about finding mistakes using a 'confusion matrix' and the differences between picking one answer, several answers, or multiple types of answers. Keep up the great work!
The one-minute story
So far the answer was yes or no. But a post office needs to read ten digits, a zoo app must pick one animal out of thousands, and a photo app wants to tag a picture with several labels at once ("beach", "dog", "sunset").
Some classifiers can only say yes/no. How do you build a ten-way digit reader from yes/no machines? Two classic tricks:
- One-versus-rest (OvR): train one machine per class: "is it a 3 or not?" "is it a 7 or not?" ... Ask all ten and take the one that shouts loudest.
- One-versus-one (OvO): train a machine for every pair of classes ("3 vs 7", "3 vs 8", ...). Let them vote.
scikit-learn picks the right trick for you automatically, but exam questions love to ask how many machines that takes. This week also separates three confusingly similar problems: multiclass, multilabel and multioutput.
By the end of this week you can:
- say which strategy (OvR or OvO) a classifier uses and count the binary classifiers;
- train a multiclass classifier and read a confusion matrix for error analysis;
- tell multiclass, multilabel and multioutput problems apart;
- preprocess labels correctly in each setting.
Words you need
- Multiclass - each example has exactly one label out of classes (a digit 0 to 9).
- Multilabel - each example can have several labels at once (a photo tagged beach + dog).
- Multioutput - several different prediction targets, each possibly multiclass (predict both the digit and its colour).
- OvR / OvA - one-versus-the-rest (one-versus-all).
- OvO - one-versus-one.
- Error analysis - studying which mistakes the model makes, to improve it.
Idea 1: Binary machines, many classes
For classes:
| Strategy | Number of binary classifiers | Each trained on | Predict by |
|---|---|---|---|
| One-versus-rest (OvR) | all the data (class = positive, everything else = negative) | the class whose classifier gives the highest score | |
| One-versus-one (OvO) | only the examples of two classes | majority vote of all the pairwise duels |
For the 10 digits: OvR = 10 classifiers, OvO = 45. For 4 classes: OvR 4, OvO 6. For 3 classes both give 3. Because for , the method that needs more than classifiers is OvO.
Why OvO at all? Each OvO classifier trains on a small subset, which is much faster for algorithms that scale badly with data size (SVMs). OvR is the default for most others.
- scikit-learn picks automatically:
SGDClassifier,LogisticRegression,LinearSVCuse OvR (or softmax) internally;SVCuses OvO internally (though by default it reports scores in the one-vs-rest shape,decision_function_shape="ovr"). - To force one: wrap any binary estimator:
from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
ovr = OneVsRestClassifier(SGDClassifier(random_state=42)).fit(X_train, y_train)
ovo = OneVsOneClassifier(SGDClassifier(random_state=42)).fit(X_train, y_train)
len(ovr.estimators_), len(ovo.estimators_) # (10, 45) on the digits data
- Some classifiers are natively multiclass and need no trick: Naive Bayes, random forests, decision trees, k-NN, multinomial logistic regression.
Idea 2: A real multiclass run
The scikit-learn digits data: 1797 images of pixels (64 features), digits 0 to 9, about 180 of each. We split 75/25 with stratify=y:
sgd = make_pipeline(StandardScaler(), SGDClassifier(random_state=42)).fit(X_train, y_train)
sgd.score(X_test, y_test) # 0.947
sgd[-1].coef_.shape # (10, 64): one row of weights per class (OvR)
sgd.decision_function(X_test[:2]).shape # (2, 10): one score per class
For each example the model computes 10 scores and predicts argmax. predict_proba is available only if the loss supports it (loss="log_loss").
Metrics for many classes. accuracy_score still works. precision_score(y, pred, average="macro") computes one precision per class and averages them; "weighted" weights by class size; "micro" pools all decisions. For multiclass you must say which average= you want (the default "binary" fails).
Idea 3: Error analysis with the confusion matrix
For classes the confusion matrix is : row = true class, column = predicted class. The diagonal is correct; off-diagonal cells are the mistakes.
cm = confusion_matrix(y_test, sgd.predict(X_test))
np.trace(cm), cm.sum() - np.trace(cm) # 426 correct, 24 wrong (of 450)
The biggest single confusion in our run: true digit 1 predicted as 8 (5 times). That tells us where to look: maybe more training data of 1s, cleaner pixel features, or a better model. To see rates rather than counts, divide each row by its sum (normalize="true"). Zeroing the diagonal before plotting makes the mistakes stand out.
Idea 4: Multiclass, multilabel, multioutput
| Problem | Targets look like | Example | Tool |
|---|---|---|---|
| Multiclass | one number per example, from classes | digit | any classifier above |
| Multilabel | several yes/no columns: matrix of 0/1 | beach? dog? | KNeighborsClassifier, RandomForest, MultiOutputClassifier, ClassifierChain |
| Multioutput | several columns, each multiclass | (digit, colour) | MultiOutputClassifier, native in trees/k-NN |
"A model predicts multiple labels (more than two) for each instance" is multilabel classification.
y_multilabel = np.c_[(y >= 7), (y % 2 == 1)].astype(int) # "is large?" and "is odd?"
knn = KNeighborsClassifier().fit(X_train, y_multilabel_train)
knn.predict(X_test[:3]) # [[0,1],[0,0],[1,1]]: two yes/no answers per example
KNeighborsClassifier supports multilabel directly. Evaluate with f1_score(y, pred, average="macro") per label.
Idea 5: Preparing the labels
| Task | Right label preprocessing |
|---|---|
| Multiclass, strings like "cat","dog" | LabelEncoder (to 0,1,2...) or leave strings (most classifiers accept them); LabelBinarizer for one-hot targets |
Multilabel sets like {"a","b"} | MultiLabelBinarizer 0/1 matrix |
| Features that are categories | OneHotEncoder / OrdinalEncoder (not LabelEncoder) |
LabelBinarizer().fit_transform([2, 0, 1, 2]) # [[0,0,1],[1,0,0],[0,1,0],[0,0,1]]
MultiLabelBinarizer().fit_transform([{"a","b"}, {"b"}, {"a","c"}])
# [[1,1,0],[0,1,0],[1,0,1]]
Solved examples
Question. A 6-class problem. How many binary classifiers does OvR train? OvO?
Solution. OvR: . OvO: .
Question. A 5-class SGDClassifier is trained on 8 features. What is coef_.shape? What is decision_function(X_new).shape for 20 new rows?
Solution. coef_: (OvR: one weight vector per class). decision_function: .
Question. (a) Tag each news article with all topics that apply (politics, sport, health ...). (b) Classify each fruit as apple, banana or orange. (c) For each photo predict the animal and its age group.
Solution. (a) Multilabel. (b) Multiclass. (c) Multioutput (two multiclass targets).
Your turn
10 classes. If each OvO classifier needs 2 seconds and each OvR classifier 30 seconds, which total is cheaper?
Need a hint?
Count the classifiers first.
Show the full solution
OvO: s. OvR: s. OvO wins here because each OvO machine sees only a small slice of the data.
"Which classification method needs more than classifiers, where is the number of classes?" (OvR, OvO, softmax regression)
Need a hint?
Compare with .
Show the full solution
OvO (for , ). OvR needs exactly and softmax regression is a single model.
In a 3-class confusion matrix [[30,2,1],[3,25,2],[0,4,28]], how many examples are there, and what is the accuracy? Which class is most often confused with which?
Need a hint?
Trace over total.
Show the full solution
Total . Correct . Accuracy . The largest off-diagonal entry is : true class 3 predicted as class 2.
Targets are [{"rock","pop"}, {"jazz"}, {"pop","jazz"}]. Which preprocessing?
Need a hint?
Several labels per example.
Show the full solution
MultiLabelBinarizer (this is a multilabel problem).
For multiclass precision you call precision_score(y, pred) and get an error. Why, and what is the fix?
Need a hint?
The default is for two classes.
Show the full solution
The default is average="binary", which assumes two classes. Pass average="macro" (or "weighted" or "micro").
Exam radar
In MLP Quiz 2: "Which of the options below represent the correct method to preprocess class labels in a multiclass setup?", "Which type of classification problem predicts multiple labels (more than 2) for each instance?" (multilabel), and "Which method of classification needs more than classifiers?" (OvO). The End Term adds OneVsRestClassifier/OneVsOneClassifier counts, decision_function shapes, and confusion-matrix error analysis.
Memorise: OvR , OvO ; SVMs prefer OvO; multilabel = several yes/no columns.
- OvO needs classifiers (not , not ).
- Multilabel multiclass: multilabel examples can have several labels at once.
LabelEncoderis for targets; useOneHotEncoderfor categorical features.- In a multiclass confusion matrix, rows are the truth.
- Multiclass
precision_scoreneedsaverage=.
Quick quiz
How many binary classifiers does one-versus-one train for 8 classes?
- 8
- 16
- 28
- 64
A model predicts several labels for each photo (beach, dog, sunset). This is...
- multiclass
- multilabel
- regression
- clustering
Which of these is a natively multiclass classifier?
- a single linear SVM
- a random forest
- a perceptron
- the sigmoid
Select all TRUE statements. (select all that apply)
- OvR trains one classifier per class on all the data.
- SVC uses one-versus-one internally.
- OvO trains K classifiers for K classes.
- LabelEncoder should be used for categorical input features.
Recap
- OvR: classifiers, take the highest score. OvO: , vote; good for SVMs. Wrap with
OneVsRestClassifier/OneVsOneClassifier. - Multiclass
coef_shape ,decision_function. Useaverage="macro"|"weighted"for metrics. - Error analysis: confusion matrix, rows = truth; find the biggest off-diagonal cells.
- Multiclass (one label of ), multilabel (several yes/no), multioutput (several multiclass).
LabelEncoder/LabelBinarizerfor labels,MultiLabelBinarizerfor label sets.
- OvR classifiers ; OvO classifiers (10 classes: 10 vs 45)
precision_score(y, pred, average="macro"|"weighted"|"micro")confusion_matrix(y_true, y_pred): rows = true, columns = predicted
- MLT week 12 - Neural networks: Multiclass classification.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 9 real questions on this week All Quiz 2 papers