DAI developerClassical machine learning· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN
Regression metrics: MAE, RMSE, R²
Be able to choose and interpret metrics for regression.
Prerequisites
Intuition
Three measures of «how wrong does the model guess?» when the answer is a number:
| Metric | Formula | Property |
|---|---|---|
| MAE | the mean of |y − ŷ| | in the same unit, every error weighs the same |
| RMSE | √(the mean of (y − ŷ)²) | in the same unit, punishes large errors hard |
| R² | 1 − SS_res/SS_tot | the share of the variation that is explained; 1 = perfect, 0 = as good as always guessing the mean |
RMSE ≥ MAE always. If RMSE is much larger than MAE there are a few large errors — go and look at them.
R² can go negative. That means the model is worse than always guessing the mean — which happens more often than you would think on test data.
Code
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
y = np.array([100, 200, 300, 400, 500])
pred = np.array([110, 190, 310, 380, 700]) # the last one is badly wrong
print(round(mean_absolute_error(y, pred), 1)) # 50.0
print(round(mean_squared_error(y, pred) ** 0.5, 1)) # 92.2 ← pulled up by the 200 error
print(round(r2_score(y, pred), 3)) # 0.575
# without the outlier:
print(round(mean_absolute_error(y[:4], pred[:4]), 1),
round(mean_squared_error(y[:4], pred[:4]) ** 0.5, 1)) # 12.5 14.6
Choosing the metric: MAE when every error costs the same per unit (delivery time in days). RMSE when large errors are disproportionately expensive (electricity grid load). And always report a baseline — R² does that for you by comparing against the mean guess.
Mastery means
- Computes and interprets MAE, RMSE and R²
- Chooses the metric according to how errors should be punished
Sign in to do the exercises and build your mastery up.
Sources
- scikit-learn User Guide (BSD-3) — BSD-3-Clause
- Wikipedia — Coefficient of determination (CC BY-SA 4.0) — CC BY-SA 4.0