Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Online ml River Base MultiOutput

From Leeroopedia


Knowledge Sources
Domains Online_Learning, Multi_Output_Learning, Base_Classes
Last Updated 2026-02-08 16:00 GMT

Overview

The MultiOutput classes define the interface for multi-label classification and multi-target regression in River, where models predict multiple outputs simultaneously.

Description

River provides two base classes for multi-output learning scenarios. MultiLabelClassifier handles multi-label classification where each example can belong to multiple classes simultaneously, with targets represented as dictionaries mapping label names to boolean values. It provides learn_one for training, predict_proba_one for probability predictions (returning nested dictionaries of label probabilities), and predict_one for discrete predictions. MultiTargetRegressor handles multi-target regression where each example has multiple numeric target values, with targets represented as dictionaries mapping target names to numeric values. Both classes extend Estimator and define abstract methods for learning and prediction.

Usage

Use MultiLabelClassifier as the parent class when implementing algorithms that predict multiple binary labels per instance, such as document topic classification or image tag prediction. Use MultiTargetRegressor for problems with multiple numeric outputs, such as predicting temperature and humidity simultaneously. All implementations must define both learn_one and either predict_proba_one (for classifiers) or predict_one (for regressors).

Code Reference

Source Location

Signature

class MultiLabelClassifier(Estimator, abc.ABC):
    """Multi-label classifier."""

    @abc.abstractmethod
    def learn_one(self, x: dict[FeatureName, typing.Any], y: dict[FeatureName, bool]) -> None

    def predict_proba_one(
        self,
        x: dict[FeatureName, typing.Any],
        **kwargs: typing.Any
    ) -> dict[FeatureName, dict[bool, float]]

    def predict_one(
        self,
        x: dict[FeatureName, typing.Any],
        **kwargs: typing.Any
    ) -> dict[FeatureName, bool]


class MultiTargetRegressor(Estimator, abc.ABC):
    """Multi-target regressor."""

    @abc.abstractmethod
    def learn_one(
        self,
        x: dict[FeatureName, typing.Any],
        y: dict[FeatureName, RegTarget],
        **kwargs: typing.Any,
    ) -> None

    @abc.abstractmethod
    def predict_one(self, x: dict[FeatureName, typing.Any]) -> dict[FeatureName, RegTarget]

Import

from river.base import MultiLabelClassifier, MultiTargetRegressor

I/O Contract

MultiLabelClassifier.learn_one

Parameter Type Description
x dict[FeatureName, Any] Dictionary of input features
y dict[FeatureName, bool] Dictionary mapping label names to boolean values (True = present, False = absent)

MultiLabelClassifier.predict_proba_one

Parameter Type Description
x dict[FeatureName, Any] Dictionary of input features
**kwargs Any Additional algorithm-specific arguments
Returns Type Description
probabilities dict[FeatureName, dict[bool, float]] Nested dictionary: label → {True: prob_true, False: prob_false}

MultiLabelClassifier.predict_one

Parameter Type Description
x dict[FeatureName, Any] Dictionary of input features
**kwargs Any Additional algorithm-specific arguments
Returns Type Description
predictions dict[FeatureName, bool] Dictionary mapping label names to predicted boolean values

MultiTargetRegressor.learn_one

Parameter Type Description
x dict[FeatureName, Any] Dictionary of input features
y dict[FeatureName, RegTarget] Dictionary mapping target names to numeric values
**kwargs Any Additional algorithm-specific arguments

MultiTargetRegressor.predict_one

Parameter Type Description
x dict[FeatureName, Any] Dictionary of input features
Returns Type Description
predictions dict[FeatureName, RegTarget] Dictionary mapping target names to predicted numeric values

Usage Examples

from river import multioutput
from river import linear_model
from river import datasets

# Multi-label classification example
# Each document can have multiple topics
model = multioutput.ClassifierChain(
    model=linear_model.LogisticRegression()
)

# Example data: document features -> multiple labels
X = [
    {'word_count': 100, 'has_code': 1.0},
    {'word_count': 50, 'has_code': 0.0},
]

Y = [
    {'python': True, 'java': False, 'tutorial': True},
    {'python': False, 'java': True, 'tutorial': False},
]

for x, y in zip(X, Y):
    # Predict label probabilities
    y_proba = model.predict_proba_one(x)
    print(f"Probabilities: {y_proba}")
    # Output: {'python': {True: 0.6, False: 0.4}, 'java': {...}, ...}

    # Predict labels
    y_pred = model.predict_one(x)
    print(f"Predictions: {y_pred}")
    # Output: {'python': True, 'java': False, 'tutorial': True}

    # Update model
    model.learn_one(x, y)

# Multi-target regression example
# Predict multiple numeric outputs
from river import neighbors

model = multioutput.RegressorChain(
    model=neighbors.KNNRegressor()
)

# Example: predict temperature and humidity from sensor data
X = [
    {'pressure': 1013.25, 'light': 500},
    {'pressure': 1015.00, 'light': 600},
]

Y = [
    {'temperature': 20.5, 'humidity': 65.0},
    {'temperature': 22.0, 'humidity': 60.0},
]

for x, y in zip(X, Y):
    # Predict all targets
    y_pred = model.predict_one(x)
    print(f"Predicted: {y_pred}")
    # Output: {'temperature': 21.2, 'humidity': 62.5}

    # Update model
    model.learn_one(x, y)

# Implementing a custom multi-label classifier
from river.base import MultiLabelClassifier

class IndependentBinaryClassifiers(MultiLabelClassifier):
    def __init__(self, base_model):
        self.base_model = base_model
        self.models = {}

    def learn_one(self, x, y):
        # Train separate classifier for each label
        for label, value in y.items():
            if label not in self.models:
                self.models[label] = self.base_model.clone()
            # Convert to binary classification
            self.models[label].learn_one(x, value)

    def predict_proba_one(self, x):
        # Get probabilities from each classifier
        probas = {}
        for label, model in self.models.items():
            label_probas = model.predict_proba_one(x)
            probas[label] = label_probas
        return probas

    def predict_one(self, x):
        # Get prediction from each classifier
        probas = self.predict_proba_one(x)
        preds = {}
        for label, label_probas in probas.items():
            if label_probas:
                preds[label] = max(label_probas, key=label_probas.get)
        return preds

# Implementing a custom multi-target regressor
from river.base import MultiTargetRegressor

class IndependentRegressors(MultiTargetRegressor):
    def __init__(self, base_model):
        self.base_model = base_model
        self.models = {}

    def learn_one(self, x, y, **kwargs):
        # Train separate regressor for each target
        for target, value in y.items():
            if target not in self.models:
                self.models[target] = self.base_model.clone()
            self.models[target].learn_one(x, value)

    def predict_one(self, x):
        # Get prediction from each regressor
        preds = {}
        for target, model in self.models.items():
            preds[target] = model.predict_one(x)
        return preds

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment