sklearn.multiclass.OneVsOneClassifier¶

class sklearn.multiclass.OneVsOneClassifier(estimator, n_jobs=None)[source]

One-vs-one multiclass strategy

This strategy consists in fitting one classifier per class pair. At prediction time, the class which received the most votes is selected. Since it requires to fit n_classes * (n_classes - 1) / 2 classifiers, this method is usually slower than one-vs-the-rest, due to its O(n_classes^2) complexity. However, this method may be advantageous for algorithms such as kernel algorithms which don’t scale well with n_samples. This is because each individual learning problem only involves a small subset of the data whereas, with one-vs-the-rest, the complete dataset is used n_classes times.

Read more in the User Guide.

Parameters
estimatorestimator object

An estimator object implementing fit and one of decision_function or predict_proba.

n_jobsint or None, optional (default=None)

The number of jobs to use for the computation. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.

Attributes
estimators_list of n_classes * (n_classes - 1) / 2 estimators

Estimators used for predictions.

classes_numpy array of shape [n_classes]

Array containing labels.

n_classes_int

Number of classes

pairwise_indices_list, length = len(estimators_), or None

Indices of samples used when training the estimators. None when estimator does not have _pairwise attribute.

Methods

 decision_function(self, X) Decision function for the OneVsOneClassifier. fit(self, X, y) Fit underlying estimators. get_params(self[, deep]) Get parameters for this estimator. partial_fit(self, X, y[, classes]) Partially fit underlying estimators predict(self, X) Estimate the best class label for each sample in X. score(self, X, y[, sample_weight]) Returns the mean accuracy on the given test data and labels. set_params(self, \*\*params) Set the parameters of this estimator.
__init__(self, estimator, n_jobs=None)[source]

Initialize self. See help(type(self)) for accurate signature.

decision_function(self, X)[source]

Decision function for the OneVsOneClassifier.

The decision values for the samples are computed by adding the normalized sum of pair-wise classification confidence levels to the votes in order to disambiguate between the decision values when the votes for all the classes are equal leading to a tie.

Parameters
Xarray-like, shape = [n_samples, n_features]
Returns
Yarray-like, shape = [n_samples, n_classes]
fit(self, X, y)[source]

Fit underlying estimators.

Parameters
X(sparse) array-like, shape = [n_samples, n_features]

Data.

yarray-like, shape = [n_samples]

Multi-class targets.

Returns
self
get_params(self, deep=True)[source]

Get parameters for this estimator.

Parameters
deepboolean, optional

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns
paramsmapping of string to any

Parameter names mapped to their values.

partial_fit(self, X, y, classes=None)[source]

Partially fit underlying estimators

Should be used when memory is inefficient to train all data. Chunks of data can be passed in several iteration, where the first call should have an array of all target variables.

Parameters
X(sparse) array-like, shape = [n_samples, n_features]

Data.

yarray-like, shape = [n_samples]

Multi-class targets.

classesarray, shape (n_classes, )

Classes across all calls to partial_fit. Can be obtained via np.unique(y_all), where y_all is the target vector of the entire dataset. This argument is only required in the first call of partial_fit and can be omitted in the subsequent calls.

Returns
self
predict(self, X)[source]

Estimate the best class label for each sample in X.

This is implemented as argmax(decision_function(X), axis=1) which will return the label of the class with most votes by estimators predicting the outcome of a decision for each possible class pair.

Parameters
X(sparse) array-like, shape = [n_samples, n_features]

Data.

Returns
ynumpy array of shape [n_samples]

Predicted multi-class targets.

score(self, X, y, sample_weight=None)[source]

Returns the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters
Xarray-like, shape = (n_samples, n_features)

Test samples.

yarray-like, shape = (n_samples) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like, shape = [n_samples], optional

Sample weights.

Returns
scorefloat

Mean accuracy of self.predict(X) wrt. y.

set_params(self, **params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns
self