Extend a binary classifier to multiclass data
A multiclass dataset where the chosen base estimator is binary-classification oriented.
Scikit-Learn · Class
OneVsRestClassifier wraps a binary classifier to solve multiclass problems by fitting one model per class against all other classes. It also supports multilabel classification through independent binary models for each label.
OneVsRestClassifier wraps a binary estimator to solve multiclass problems by training a separate model for each class against all remaining classes. It can also handle multilabel targets when fit receives a two-dimensional binary indicator matrix, training one independent binary model per label.
class sklearn.multiclass.OneVsRestClassifier(estimator, *, n_jobs=None, verbose=0)| Name | Type | Description |
|---|---|---|
estimator | estimator object | The binary classifier to clone and fit for each class or label. |
n_jobs | int or None, default=None | Number of jobs to run in parallel; None means one job unless a backend context changes it. |
verbose | int, default=0 | Verbosity level for fitting and prediction operations. |
A fitted OneVsRestClassifier instance when fit is called; its prediction methods produce class or multilabel outputs.
from sklearn.datasets import load_iris
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
X, y = load_iris(return_X_y=True)
model = OneVsRestClassifier(LinearSVC(random_state=0))
model.fit(X, y)
predictions = model.predict(X[:3])
print(predictions)This trains one linear binary classifier per iris class and predicts the classes of three samples.
A multiclass dataset where the chosen base estimator is binary-classification oriented.
A target represented by a sample-by-label 0/1 matrix.
A need to inspect class-specific decision boundaries or coefficients.
It trains one binary classifier for every class. Each classifier separates its own class from all other classes, producing an OvR multiclass model.
Follow-up: How does this extend to multilabel classification? Pass a binary indicator matrix; the wrapper independently trains one binary classifier for each label.
The wrapped estimator must be a classifier compatible with the estimator interface. The wrapper delegates fitting and prediction to one cloned binary estimator per class or label.
Follow-up: What must the base estimator provide for probability output? It must implement predict_proba; otherwise the wrapper cannot expose probability predictions.
For multilabel data, each row of y represents a sample and each column represents a label. A 1 in position [i, j] means label j is present in sample i.
Follow-up: Why can OvR be useful for multilabel tasks? Each label is modeled independently, so the approach directly implements binary relevance.
OneVsOneClassifierOutputCodeClassifierClassifierChainsklearn.multiclass