Implementation:Online ml River Multiclass OneVsOne
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Multi_Class_Classification, Ensemble_Methods |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
One-vs-One (OvO) strategy solves multi-class problems by training a binary classifier for each pair of classes and combining their votes to make final predictions.
Description
OvO creates k(k-1)/2 binary classifiers where k is the number of classes, with one classifier for each pair of classes. New classifiers are instantiated dynamically as new classes appear in the online stream. When learning from a sample with label y, only the k-1 classifiers involving class y are updated (those distinguishing y from each other class). For prediction, all classifiers vote and the class receiving the most votes wins. Each pairwise classifier is stored in a dictionary with sorted pairs as keys (e.g., (class_a, class_b) where class_a < class_b lexicographically). This approach efficiently handles online learning where the number of classes is not known upfront.
Usage
Use One-vs-One when you have a multi-class problem and want to use binary classifiers. OvO often works better than OvR when classes are not well-separated or when the binary classification problem between pairs is simpler than the full multi-class problem. Training is more efficient than it appears: each update only trains k-1 classifiers, not all k(k-1)/2. However, prediction requires querying all classifiers, so it can be slower than OvR for problems with many classes. Best suited for problems with moderate numbers of classes (under 20).
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/multiclass/ovo.py
Signature
class OneVsOneClassifier(
classifier: base.Classifier
)
Import
from river import multiclass
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| x | dict | Feature dictionary |
| y | Any | Class label (any hashable type) |
| Method | Return Type | Description |
|---|---|---|
| predict_one(x) | Any | Class with most votes from pairwise classifiers |
| learn_one(x, y) | None | Updates k-1 relevant pairwise classifiers |
Usage 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()
ovo = multiclass.OneVsOneClassifier(linear_model.LogisticRegression())
model = scaler | ovo
metric = metrics.MacroF1()
result = evaluate.progressive_val_score(dataset, model, metric)
print(result) # MacroF1: 80.76%