OneVsRestClassifier¶
One-vs-the-rest (OvR) multiclass strategy.
This strategy consists in fitting one binary classifier per class. Because we are in a streaming context, the number of classes isn't known from the start. Hence, new classifiers are instantiated on the fly. Likewise, the predicted probabilities will only include the classes seen up to a given point in time.
Note that this classifier supports mini-batches as well as single instances.
The computational complexity for both learning and predicting grows linearly with the number of classes. If you have a very large number of classes, then you might want to consider using an multiclass.OutputCodeClassifier
instead.
Parameters¶
-
classifier ('base.Classifier')
A binary classifier, although a multi-class classifier will work too.
Attributes¶
-
classifiers (dict)
A mapping between classes and classifiers.
Examples¶
>>> from river import datasets
>>> from river import evaluate
>>> from river import linear_model
>>> from river import metrics
>>> from river import multiclass
>>> from river import preprocessing
>>> dataset = datasets.ImageSegments()
>>> scaler = preprocessing.StandardScaler()
>>> ovr = multiclass.OneVsRestClassifier(linear_model.LogisticRegression())
>>> model = scaler | ovr
>>> metric = metrics.MacroF1()
>>> evaluate.progressive_val_score(dataset, model, metric)
MacroF1: 77.46%
This estimator also also supports mini-batching.
>>> for X in pd.read_csv(dataset.path, chunksize=64):
... y = X.pop('category')
... y_pred = model.predict_many(X)
... model = model.learn_many(X, y)
Methods¶
learn_many
learn_one
Update the model with a set of features x
and a label y
.
Parameters
- x
- y
Returns
self
predict_many
predict_one
Predict the label of a set of features x
.
Parameters
- x (dict)
Returns
typing.Union[bool, str, int, NoneType]: The predicted label.
predict_proba_many
predict_proba_one
Predict the probability of each label for a dictionary of features x
.
Parameters
- x
Returns
A dictionary that associates a probability which each label.