Sub topic 14
Extending SVMs to more than two classes
This chapter assumes you know the soft-margin SVM, the role of C, and how to fit SVC in scikit-learn. An SVM is a binary classifier by construction: its decision function has one sign, positive or negative. Multiclass use means combining several binary SVMs.
Two decomposition strategies
One-vs-rest (OvR) trains one classifier per class. Classifier k treats class k as positive and all other samples as negative. For K classes you train K models. At prediction time you pick the class whose classifier gives the highest decision score.
One-vs-one (OvO) trains one classifier for every pair of classes. For K classes you train K(K-1)/2 models. Each model votes for one of its two classes, and the class with the most votes wins.
| Strategy | Models trained | Training data per model | Prediction rule |
|---|---|---|---|
| One-vs-rest | K | All samples | Highest decision score |
| One-vs-one | K(K-1)/2 | Only two classes | Majority vote |
OvO trains more models, but each one sees less data. Kernel SVM training cost grows faster than linearly with sample count, so many small problems can be cheaper than K large ones.
What scikit-learn does
SVC always uses one-vs-one internally, because that is how libsvm works. By default it still reports decision_function values in an OvR shape, controlled by decision_function_shape="ovr". These scores are built from the OvO votes. Set decision_function_shape="ovo" to see the raw pairwise scores.
LinearSVC uses one-vs-rest by default. To force a strategy with any binary estimator, wrap it in OneVsRestClassifier or OneVsOneClassifier.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.multiclass import OneVsRestClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
ovo = make_pipeline(StandardScaler(), SVC(kernel="rbf"))
ovr = make_pipeline(
StandardScaler(), OneVsRestClassifier(SVC(kernel="rbf"))
)
ovo.fit(X_train, y_train)
ovr.fit(X_train, y_train)
print("OvO accuracy:", ovo.score(X_test, y_test))
print("OvR accuracy:", ovr.score(X_test, y_test))
print("OvO scores shape:", ovo.decision_function(X_test).shape)
print("pairwise models:", len(ovo[-1].intercept_))With three classes, the last line prints 3, which is K(K-1)/2.
Common errors
- Comparing OvR scores across models. Each binary SVM has its own scale, so its scores are not calibrated against the others. The argmax can pick the wrong class when scales differ.
- Ties in voting. With OvO, two classes can receive the same number of votes.
SVCbreaks ties by picking the lower class index, which is arbitrary. Settingbreak_ties=Trueuses the decision scores instead. - Class imbalance in OvR. Each "rest" group is much larger than its "one" group when K is big. Consider
class_weight="balanced". - Assuming
decision_function_shape="ovr"means OvR training. It changes only the output shape.
Exercise
A dataset has 10 classes. How many binary SVMs does one-vs-one train, and how many does one-vs-rest train?
Show answer
One-vs-one trains 10 x 9 / 2 = 45 models. One-vs-rest trains 10 models.
Exercise
You call SVC(decision_function_shape="ovr") and the model still trains pairwise classifiers. Why?
Show answer
The parameter only controls the shape of the decision_function output. SVC is built on libsvm, which always trains one-vs-one models internally.