mojolearn.OneVsRestClassifier#

class mojolearn.OneVsRestClassifier(estimator, *, n_jobs=None, verbose=0)[source]#

Bases: _TreesWrapperBase

Multiclass classification with one binary classifier per class.

Follows scikit-learn’s OneVsRestClassifier: fit trains one clone of estimator per class (that class against the rest), or a single clone when there are two classes. predict takes the class whose clone scores highest, using decision_function when the estimator has one, else the positive-class column of predict_proba. The classes fit one after another.

Differs from scikit-learn: in predict_proba, a row whose per-class probabilities are all 0 gets uniform probabilities (scikit-learn divides 0 by 0).

Parameters:
  • estimator (estimator) – A mojolearn binary classifier; it is cloned, never fitted in place.

  • n_jobs (None) – Only None is accepted; any other value raises NotImplementedError.

  • verbose (int, default 0) – Only 0 is accepted; any other value raises NotImplementedError.

Variables:
  • estimators_ (list) – The fitted clones, one per class (one in total for two classes).

  • classes_ (array) – The class labels seen in fit, sorted.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = (3 * X[:, 0]).astype(np.int32)
>>> model = mojolearn.OneVsRestClassifier(mojolearn.LogisticRegression()).fit(X, y)
>>> model.predict_proba(X[:5]).shape
(5, 3)
__init__(estimator, *, n_jobs=None, verbose=0)[source]#

Methods

fit(X, y)

Fit one binary clone per class.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict the class whose binary classifier scores highest.

predict_proba(X)

Class probabilities.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y)[source]#

Fit one binary clone per class.

Parameters:
  • X (array of shape (n_samples, n_features)) – Training data, converted to float32.

  • y (array of shape (n_samples,)) – Class labels; fewer than two classes raise ValueError.

Returns:

self

predict(X)[source]#

Predict the class whose binary classifier scores highest.

Returns:

array of shape (n_samples,) – Labels from classes_.

predict_proba(X)[source]#

Class probabilities.

Each class’s positive-class probability from its clone, with every row normalized to sum to 1 when there are more than two classes. For two classes the rows are (1 - p, p).

Returns:

array of shape (n_samples, n_classes), float64