One-vs-the-rest (OvR) multiclass/multilabel strategy
Also known as one-vs-all, this strategy consists in fitting one classifier per class. For each classifier, the class is fitted against all the other classes. In addition to its computational efficiency (only `n_classes` classifiers are needed), one advantage of this approach is its interpretability. Since each class is represented by one and one classifier only, it is possible to gain knowledge about the class by inspecting its corresponding classifier. This is the most commonly used strategy for multiclass classification and is a fair default choice.
This strategy can also be used for multilabel learning, where a classifier is used to predict multiple labels for instance, by fitting on a 2-d matrix in which cell i, j
is 1 if sample i has label j and 0 otherwise.
In the multilabel learning literature, OvR is also known as the binary relevance method.
Read more in the :ref:`User Guide <ovr_classification>`.
Parameters ---------- estimator : estimator object An estimator object implementing :term:`fit` and one of :term:`decision_function` or :term:`predict_proba`.
n_jobs : int or None, optional (default=None) The number of jobs to use for the computation. ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. ``-1`` means using all processors. See :term:`Glossary <n_jobs>` for more details.
Attributes ---------- estimators_ : list of `n_classes` estimators Estimators used for predictions.
classes_ : array, shape = `n_classes`
Class labels.
n_classes_ : int Number of classes.
label_binarizer_ : LabelBinarizer object Object used to transform multiclass labels to binary labels and vice-versa.
multilabel_ : boolean Whether a OneVsRestClassifier is a multilabel classifier.
Examples -------- >>> import numpy as np >>> from sklearn.multiclass import OneVsRestClassifier >>> from sklearn.svm import SVC >>> X = np.array(
... [10, 10],
... [8, 10],
... [-5, 5.5],
... [-5.4, 5.5],
... [-20, -20],
... [-15, -20]
...
) >>> y = np.array(0, 0, 1, 1, 2, 2
) >>> clf = OneVsRestClassifier(SVC()).fit(X, y) >>> clf.predict([-19, -20], [9, 9], [-5, 5]
) array(2, 0, 1
)