Implementation:Online ml River Metrics MAPE
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Evaluation_Metrics, Regression |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Mean Absolute Percentage Error measuring average percentage deviation of predictions from true values.
Description
MAPE computes the mean of absolute percentage errors: 100 × mean(|y_true - y_pred| / |y_true|). It expresses error as a percentage of the true value, making it scale-independent and useful for comparing performance across different datasets or ranges. Returns 0 when y_true is 0 to avoid division by zero. Lower values indicate better performance.
Usage
Use MAPE when you need scale-independent error metrics to compare model performance across datasets with different value ranges, or when stakeholders prefer percentage-based error reports. MAPE is intuitive (e.g., "5% error") but becomes problematic when true values are near zero or when the dataset contains zeros. For such cases, consider SMAPE or other alternatives.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/metrics/mape.py
Signature
class MAPE(metrics.base.MeanMetric, metrics.base.RegressionMetric):
def __init__(self):
pass
Import
from river import metrics
I/O Contract
| Method | Parameters | Returns | Description |
|---|---|---|---|
| update | y_true (float), y_pred (float), [w] | None | Updates metric with true and predicted values |
| get | - | float | Returns mean absolute percentage error (lower is better) |
Usage Examples
from river import metrics
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
metric = metrics.MAPE()
for yt, yp in zip(y_true, y_pred):
metric.update(yt, yp)
print(metric)
# MAPE: 32.738095
# Interpretation: On average, predictions deviate by 32.74% from true values
# Calculation:
# |3-2.5|/|3| = 0.1667 (16.67%)
# |-0.5-0|/|-0.5| = 1.0 (100%)
# |2-2|/|2| = 0 (0%)
# |7-8|/|7| = 0.1429 (14.29%)
# MAPE = 100 × (0.1667 + 1.0 + 0 + 0.1429) / 4 = 32.74%
# MAPE is scale-independent
y_true_scaled = [30, -5, 20, 70] # 10x larger
y_pred_scaled = [25, 0, 20, 80]
metric_scaled = metrics.MAPE()
for yt, yp in zip(y_true_scaled, y_pred_scaled):
metric_scaled.update(yt, yp)
print(metric_scaled)
# MAPE: 32.738095 (same as before despite different scales)
# Warning: MAPE is undefined when y_true=0
# This implementation returns 0 for such cases