Implementation:Online ml River Multiclass OutputCodeClassifier
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Multi_Class_Classification, Error_Correcting_Codes |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Output-Code Classifier uses error-correcting output codes to solve multi-class problems by training multiple binary classifiers, one per bit in the code, providing robustness through redundancy.
Description
This strategy converts multi-class classification to binary classification by encoding each class as a binary code of specified length (code_size). One binary classifier is trained for each bit position in the code. When a new class appears, a code is generated either randomly or by streaming through all possible codes in random order (exact method). During training, each classifier learns to predict its corresponding bit of the class code. For prediction, all classifiers output probabilities which form a bit vector, compared against all known class codes using Manhattan distance. The closest code determines the predicted class. The redundancy in longer codes provides error correction capabilities.
Usage
Use Output-Code Classifier when you want robustness in multi-class problems through redundancy and error correction. It is particularly useful when you have many classes or when individual classifiers may be unreliable. Set code_size to balance between computational cost (more classifiers) and error correction capability (longer codes). Use coding_method='exact' for guaranteed unique codes per class (memory intensive for large codes) or 'random' for on-the-fly generation (may produce duplicates but uses less memory). Typically more robust than OvR/OvO but computationally more expensive.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/multiclass/occ.py
Signature
class OutputCodeClassifier(
classifier: base.Classifier,
code_size: int,
coding_method: str = "random",
seed: int | None = None,
)
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 | Predicted class based on nearest code |
| learn_one(x, y) | None | Trains all bit classifiers with class code |
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()
occ = multiclass.OutputCodeClassifier(
classifier=linear_model.LogisticRegression(),
code_size=10,
coding_method='random',
seed=1
)
model = scaler | occ
metric = metrics.MacroF1()
result = evaluate.progressive_val_score(dataset, model, metric)
print(result) # MacroF1: 79.58%