Sub topic 7
One-vs-rest and one-vs-one strategies
This chapter assumes you can train a binary classifier such as logistic regression, and that you know the multiclass target shape from the earlier chapters. Some classifiers only separate two classes. Two strategies split a K-class problem into binary problems so those classifiers can still be used.
One-vs-rest
One-vs-rest (OvR, also called one-vs-all) trains one binary classifier per class. Classifier k treats class k as positive and every other class as negative. For K classes you train K models.
To predict, you score a sample with every classifier and pick the class whose classifier gives the highest score. This is an argmax over K independent scores.
One-vs-one
One-vs-one (OvO) trains one binary classifier for every pair of classes. That gives K(K-1)/2 models. Each model sees only the samples from its two classes.
To predict, each model votes for one of its two classes. The class with the most votes wins. scikit-learn breaks ties using the summed confidence values.
Running both in scikit-learn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=0
)
for wrapper in (OneVsRestClassifier, OneVsOneClassifier):
model = wrapper(LogisticRegression(max_iter=1000)).fit(X_train, y_train)
print(wrapper.__name__, len(model.estimators_), "binary models,",
"test accuracy", round(model.score(X_test, y_test), 3))With three classes, both strategies train three models. The counts diverge as K grows:
| Classes (K) | One-vs-rest models | One-vs-one models |
|---|---|---|
| 3 | 3 | 3 |
| 10 | 10 | 45 |
| 100 | 100 | 4,950 |
Trade-offs and common errors
- Training cost. OvR trains K models, each on the full dataset. OvO trains many more models, but each uses only two classes' samples. OvO is often faster for algorithms whose cost grows quickly with sample count, such as kernel SVMs.
- Imbalance in OvR. Each OvR problem pits one class against K-1 others. With many classes, the positive class is a small minority in every model.
- Score comparability in OvR. The K classifiers are trained separately, so their scores need not share a scale. The argmax can then favor an over-confident model.
- Probabilities.
OneVsRestClassifier.predict_probanormalizes the K scores so they sum to 1.OneVsOneClassifierhas nopredict_proba, onlydecision_function. - Wrapping a native multiclass model.
LogisticRegressionalready handles multiclass natively with a softmax. Wrapping it in OvR or OvO gives a different model, not a better one by default. Use the wrappers when the base estimator is binary only, or when you want to compare.
Exercise
A problem has 20 classes. How many binary models does one-vs-rest train, and how many does one-vs-one train?
Show answer
One-vs-rest trains 20 models. One-vs-one trains 20 × 19 / 2 = 190 models.
Exercise
Why can the argmax over one-vs-rest scores pick the wrong class even when every binary classifier is individually accurate?
Show answer
The classifiers are trained separately, so their scores may be on different scales or be over-confident. A high score from one model does not mean it is more certain than a lower score from another.