Implementation:Online ml River Metrics R2
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Evaluation_Metrics, Regression |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
R² (coefficient of determination) measuring the proportion of variance in the dependent variable predictable from independent variables.
Description
R2 computes the coefficient of determination, quantifying how well the model explains the variance in the target variable. The formula is R² = 1 - (RSS / TSS) where RSS is residual sum of squares and TSS is total sum of squares. Values range from -∞ to 1, where 1 indicates perfect prediction, 0 means the model performs no better than predicting the mean, and negative values indicate worse-than-mean performance. Returns 0 when fewer than 2 samples have been observed.
Usage
Use R² when you want to understand what proportion of the target variable's variance your model explains. It's particularly useful for comparing models or assessing goodness of fit. Unlike error metrics (MAE, MSE), R² is scale-independent and bounded above at 1. However, R² can be negative for poor models, unlike R² in batch learning which is typically non-negative due to intercept fitting.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/metrics/r2.py
Signature
class R2(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 R² score (-∞ to 1.0, higher is better) |
Note: Unlike most regression metrics, bigger_is_better returns True for R².
Usage Examples
from river import metrics
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
metric = metrics.R2()
for yt, yp in zip(y_true, y_pred):
metric.update(yt, yp)
print(metric.get())
# 0.0
# 0.9183
# 0.9230
# 0.9486
print(metric)
# R2: 0.948618
# Interpretation:
# R² = 0.9486 means the model explains 94.86% of variance
# in the target variable, indicating excellent fit
# R² = 1: Perfect predictions
# R² = 0: Model performs as well as predicting mean
# R² < 0: Model performs worse than predicting mean
# Example with poor predictions
y_true_bad = [1, 2, 3, 4, 5]
y_pred_bad = [5, 4, 3, 2, 1] # Completely inverted
metric_bad = metrics.R2()
for yt, yp in zip(y_true_bad, y_pred_bad):
metric_bad.update(yt, yp)
print(f"Poor model R²: {metric_bad.get():.3f}")
# R² < 0 indicates worse than simply predicting the mean
# Example with mean predictions (baseline)
mean_val = sum(y_true_bad) / len(y_true_bad)
y_pred_mean = [mean_val] * len(y_true_bad)
metric_baseline = metrics.R2()
for yt, yp in zip(y_true_bad, y_pred_mean):
metric_baseline.update(yt, yp)
print(f"Mean predictor R²: {metric_baseline.get():.3f}")
# R² ≈ 0 for predicting the mean