Implementation:Online ml River Metrics Base
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Evaluation_Metrics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Base classes for all metrics in River's online learning evaluation framework.
Description
This module defines the abstract base classes for River's metric system, including Metric (mother class for all metrics), ClassificationMetric (for binary and multi-class classification), BinaryMetric and MultiClassMetric (specialized classification metrics), RegressionMetric (for regression tasks), ClusteringMetric (for clustering evaluation), Metrics (a container for handling multiple metrics simultaneously), WrapperMetric (for wrapping other metrics), and MeanMetric (a utility for metrics that are running averages). These classes provide the foundational structure for incremental metric computation with update and revert operations.
Usage
These base classes are used internally by River and are rarely instantiated directly. Developers creating custom metrics should inherit from the appropriate base class to ensure compatibility with River's evaluation framework.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/metrics/base.py
Signature
class Metric(base.Base, abc.ABC):
"""Mother class for all metrics."""
@abc.abstractmethod
def update(self, y_true, y_pred) -> None:
"""Update the metric."""
@abc.abstractmethod
def revert(self, y_true, y_pred) -> None:
"""Revert the metric."""
@abc.abstractmethod
def get(self) -> float:
"""Return the current value of the metric."""
@property
@abc.abstractmethod
def bigger_is_better(self) -> bool:
"""Indicate if a high value is better than a low one or not."""
class ClassificationMetric(Metric):
"""Mother class for all classification metrics."""
def __init__(self, cm=None):
pass
def update(self, y_true, y_pred, w=1.0) -> None:
pass
class BinaryMetric(ClassificationMetric):
"""Mother class for all binary classification metrics."""
def __init__(self, cm=None, pos_val=True):
pass
class MultiClassMetric(ClassificationMetric):
"""Mother class for all multi-class classification metrics."""
pass
class RegressionMetric(Metric):
"""Mother class for all regression metrics."""
@abc.abstractmethod
def update(self, y_true: float, y_pred: float) -> None:
"""Update the metric."""
class ClusteringMetric(base.Base, abc.ABC):
"""Mother class of all internal clustering metrics."""
@abc.abstractmethod
def update(self, x, y_pred, centers, w=1.0) -> None:
"""Update the metric."""
Import
from river.metrics import base
I/O Contract
| Method | Parameters | Returns | Description |
|---|---|---|---|
| update | y_true, y_pred, [w] | None | Updates the metric with a new observation |
| revert | y_true, y_pred, [w] | None | Reverts the effect of a previous observation |
| get | - | float | Returns the current metric value |
| works_with | model | bool | Checks if metric is compatible with a model |
Usage Examples
from river import metrics
# Example: Creating a custom metric by inheriting from base classes
class CustomAccuracy(metrics.base.ClassificationMetric):
def __init__(self, cm=None):
super().__init__(cm)
def get(self):
try:
return self.cm.total_true_positives / self.cm.n_samples
except ZeroDivisionError:
return 0.0
# Using the Metrics container to track multiple metrics
metric = metrics.Accuracy() + metrics.Precision()
for y_true, y_pred in [(True, True), (False, True), (True, True)]:
metric.update(y_true, y_pred)
print(metric)
# Accuracy: 66.67%
# Precision: 66.67%