Implementation:Online ml River Multioutput MultiClassEncoder
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Multi_Label_Classification, Label_Encoding |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
MultiClassEncoder transforms multi-label classification into standard multi-class classification by encoding each unique label combination as a single class.
Description
This wrapper converts multi-label problems into multi-class problems by treating each unique combination of labels as a distinct class. When a new label set is encountered, it is converted to a tuple (sorted for consistency), assigned an integer code, and stored in a bidirectional mapping. The underlying multi-class classifier is trained with these codes. During prediction, the classifier outputs a code which is decoded back to the original label set with probabilities. This approach works online by dynamically creating new codes as new label combinations appear in the stream. The encoder maintains both forward (label_set -> code) and reverse (code -> label_set) mappings.
Usage
Use MultiClassEncoder when you have a multi-label problem but want to leverage a multi-class classifier that cannot handle multi-label data directly. It works best when the number of unique label combinations is manageable (not exponentially large). The approach preserves label correlations since each combination is treated atomically. However, it may struggle with rare label combinations that appear infrequently in the stream. Suitable for problems with structured label patterns rather than arbitrary label subsets.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/multioutput/encoder.py
Signature
class MultiClassEncoder(
model: base.Classifier
)
Import
from river import multioutput
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| x | dict | Feature dictionary |
| y | dict | Dictionary mapping label names to boolean values |
| Method | Return Type | Description |
|---|---|---|
| predict_one(x) | dict | Predicted label set (decoded) |
| predict_proba_one(x) | dict | Probabilities for each label |
| learn_one(x, y) | None | Encodes and trains on label combination |
Usage Examples
from river import forest
from river import metrics
from river import multioutput
from river.datasets import synth
dataset = synth.Logical(seed=42, n_tiles=100)
model = multioutput.MultiClassEncoder(
model=forest.ARFClassifier(seed=7)
)
metric = metrics.multioutput.MicroAverage(metrics.Jaccard())
for x, y in dataset:
y_pred = model.predict_one(x)
y_pred = {k: y_pred.get(k, 0) for k in y}
metric.update(y, y_pred)
model.learn_one(x, y)
print(metric) # MicroAverage(Jaccard): 95.10%