Implementation:Online ml River Metrics MicroAverage
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Evaluation_Metrics, Multi_Output_Learning |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Micro-average wrapper aggregating all outputs into a single metric by updating one shared metric instance.
Description
MicroAverage wraps any single-output metric to work with multi-output problems by maintaining just one metric instance that is updated with values from all outputs. Unlike MacroAverage which computes per-output metrics and averages them, MicroAverage treats all predictions across all outputs as coming from a single task. This weights the metric by the total number of predictions, making frequent outputs contribute more to the final score.
Usage
Use MicroAverage when you want the overall score to be weighted by the number of predictions per output, giving more importance to frequently occurring outputs. This is appropriate when outputs have different frequencies and you want the metric to reflect overall system performance weighted by actual usage. In multi-label classification, MicroAverage emphasizes performance on common labels over rare ones.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/metrics/multioutput/micro.py
Signature
class MicroAverage(MultiOutputMetric, metrics.base.WrapperMetric):
def __init__(self, metric):
# metric: Any classification or regression metric
pass
Import
from river import metrics
I/O Contract
| Method | Parameters | Returns | Description |
|---|---|---|---|
| update | y_true (dict), y_pred (dict), [w] | None | Updates single shared metric with all output values |
| get | - | float | Returns the shared metric's value |
Usage Examples
from river import metrics
# Wrap F1 score for multi-output micro-averaging
micro_f1 = metrics.multioutput.MicroAverage(metrics.F1())
y_true = [
{0: False, 1: True, 2: True},
{0: True, 1: True, 2: False},
{0: True, 1: False, 2: True},
]
y_pred = [
{0: False, 1: True, 2: True}, # All correct
{0: True, 1: False, 2: False}, # Label 1 wrong
{0: False, 1: False, 2: True}, # Label 0 wrong
]
for yt, yp in zip(y_true, y_pred):
micro_f1.update(yt, yp)
print(micro_f1)
# Single F1 score computed across all outputs globally
# Compare Micro vs Macro averaging
macro_f1 = metrics.multioutput.MacroAverage(metrics.F1())
for yt, yp in zip(y_true, y_pred):
macro_f1.update(yt, yp)
print(f"Micro F1: {micro_f1.get():.2%}")
print(f"Macro F1: {macro_f1.get():.2%}")
# Micro: Weighted by total predictions
# Macro: Equal weight to each output
# Regression example
micro_mae = metrics.multioutput.MicroAverage(metrics.MAE())
y_true_reg = [
{0: 1.0, 1: 2.0, 2: 3.0},
{0: 2.0, 1: 3.0, 2: 4.0},
]
y_pred_reg = [
{0: 1.1, 1: 2.2, 2: 2.9},
{0: 2.1, 1: 2.8, 2: 4.2},
]
for yt, yp in zip(y_true_reg, y_pred_reg):
micro_mae.update(yt, yp)
print(f"Micro MAE: {micro_mae.get():.3f}")
# Global MAE across all outputs and samples
# Access the underlying metric
print(f"Underlying metric: {micro_mae.metric}")