Regression metrics: RMSE, MAE and R²
Each metric answers a different question about errors. Learn what RMSE, MAE, R² and MAPE reward, and why the metric and the loss should match.
What runs in CodeSplash
- LightGBM Verified in Python and R
- Baseline (no features) Verified in Python and R
You will be able to
- Compute RMSE and MAE, and say what each is sensitive to
- Read R² against a baseline
- Match the metric and loss to the question, and spot when MAPE misleads
Read first
Five rows
ReadSuppose a model's errors on five rows are 1, 1, 1, 1 and 16.
MAEMAEThe average absolute prediction error in the outcome's units; less sensitive to big misses than RMSE. is the typical miss. RMSERMSEThe typical prediction error in the outcome's units, weighting large errors more. squares first, so one big miss dominates. RMSE is never smaller than MAE, and a large gap between them says a few big misses are doing the damage.
Both are in the target's units, which makes them easy to explain.
Which metric moved?
PredictPredict first
You fix that one row's error, so 16 becomes 1. Which metric improves by the larger factor?
Reveal
RMSE, from about to 1, while MAE falls from 4 to 1. If big misses are what hurts, as they are in many budgets, RMSE is the right lens. If each unit of error costs the same however large, use MAE.
R² is a comparison
ReadR²R squaredThe share of the outcome's variance explained by the model, from 0 to 1. compares the model's squared error with a baseline that predicts the mean:
For ozone, baseline RMSE 34.6 and linear RMSE 22.9 give roughly . A forest, at 19.0, takes that to about .
On held-out rows, can go below zero: the model did worse than predicting the mean.
Look at the errors
ExplorePoints on the diagonal are perfect. Look for a pattern in the misses. Do they grow with the prediction? Does the model flatten the extremes?
View data
| sale price (k€) | predicted price (k€) | split |
|---|---|---|
| 291.40 | 289.60 | test |
| 174.30 | 132.80 | validation |
| 361.60 | 381 | validation |
| 189.50 | 261.60 | validation |
| 415.20 | 381.80 | validation |
| 253.70 | 322.20 | test |
| 412.90 | 366.50 | validation |
| 392.10 | 368.20 | validation |
| 425 | 344.20 | validation |
| 373.40 | 372.50 | validation |
| 93.300 | 140.10 | test |
| 140.80 | 162.90 | validation |
| 131.60 | 181.40 | validation |
| 428.10 | 376.50 | validation |
| 71.800 | 89.800 | validation |
| 301 | 214.80 | test |
| 413.30 | 371.40 | validation |
| 364.70 | 337.70 | validation |
| 287.30 | 357.30 | validation |
| 160.30 | 190.50 | validation |
| 392.40 | 415.20 | test |
| 273.60 | 319.70 | validation |
| 156.40 | 236.10 | validation |
| 107.90 | 120.90 | validation |
| 113 | 86.700 | validation |
| 49.900 | 102.70 | test |
| 391.90 | 392.80 | validation |
| 251.60 | 242.80 | validation |
| 141.10 | 189.90 | validation |
| 141 | 233.20 | validation |
| 177.20 | 200.10 | test |
| 237.80 | 274.70 | validation |
| 205.10 | 304.60 | validation |
| 300.10 | 345.50 | validation |
| 371.20 | 404.80 | validation |
| 410.10 | 362.30 | test |
| 385.90 | 341.50 | validation |
| 231.70 | 192.40 | validation |
| 230.60 | 208.50 | validation |
| 336.90 | 302.10 | validation |
| 366.10 | 328.20 | test |
| 222 | 281.10 | validation |
| 390.50 | 370.10 | validation |
| 327.60 | 310 | validation |
| 423.30 | 393 | validation |
| 301.70 | 306.50 | test |
| 329.70 | 317.20 | validation |
| 460.10 | 325.50 | validation |
| 248.70 | 214.70 | validation |
| 194.90 | 209 | validation |
| 243.70 | 215 | test |
| 398.30 | 412.80 | validation |
| 176.70 | 179.80 | validation |
| 182.30 | 148 | validation |
| 240.30 | 288.10 | validation |
| 263.40 | 323 | test |
| 425.50 | 396.80 | validation |
| 98.900 | 97.800 | validation |
| 371.80 | 345.70 | validation |
| 95.300 | 109.50 | validation |
First 60 of 420 rows.
A summary number hides all of this. The Evaluation tab also lists residual quantiles, and a run's residual checks test whether errors grow with the prediction.
Match the loss to the question
ReadSquared error targets the mean, absolute error targets the median, and a quantile loss targets a chosen quantile. So choose the loss from the question, not from habit.
LightGBM with absolute error (Regression loss set to Absolute error (median)) on wind and temperature has a cross-validated RMSE of 22.6. Set to the quantile on four features, it has 20.3 on the same scale. These runs aim at different things, so a lower RMSE doesn't mean a better fit for their purpose.
Percentages
PredictPredict first
MAPE divides each error by the actual value. Two rows: the actual is 1 and the prediction 2, and the actual is 1,000 and the prediction 1,001. Both miss by 1. What's the MAPE?
Reveal
About 50%: . One tiny actual dominates. MAPE explodes near zero, can't handle zeros at all, and punishes over-prediction more than under-prediction. It suits strictly positive targets that stay well away from zero, such as prices.
Check your understanding
CheckpointCheck yourself
Four errors: 3, 4, 0 and 0. What is the RMSE?
Show the answer
Answer: 2.5
, and . The MAE is . Identical errors would make the two equal.
Check yourself
A model has a held-out of . What does that mean?
Show the answer
Answer: a
Outside the training rows nothing stops the model being worse than the mean. It's a strong warning.
Check yourself
Delivery delays have a few huge outliers. You want a typical-case estimate that those outliers won't drag. Which pair fits?
Show the answer
Answer: b
Absolute error targets the median and isn't dominated by large misses. If the outliers are what you care about, the opposite choice is right.
Check yourself
Daily sales are often exactly zero. Why is MAPE a poor choice?
Show the answer
Answer: a
Scale-free metrics that handle zeros, such as a scaled error against a naive forecast, are safer here.
Choose your metric
In the appIn Setup, Primary metric ranks the leaderboard and drives tuning. For numbers you can pick RMSE, MAE or R². Every run also reports the others: open Overview to see them, and Evaluation for predicted-versus-actual and residuals.
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.impute import SimpleImputer
from sklearn.metrics import mean_absolute_error, r2_score, root_mean_squared_error
from sklearn.model_selection import cross_val_predict
from sklearn.pipeline import make_pipeline
df = pd.read_csv("airquality.csv").dropna(subset=["Ozone"])
X, y = df[["Solar.R", "Wind", "Temp", "Month"]], df["Ozone"]
m = make_pipeline(SimpleImputer(strategy="median"), RandomForestRegressor(300, random_state=1))
p = cross_val_predict(m, X, y, cv=5)
print(root_mean_squared_error(y, p), mean_absolute_error(y, p), r2_score(y, p))library(ranger)
df <- airquality[!is.na(airquality$Ozone), ]
df$Solar.R[is.na(df$Solar.R)] <- median(df$Solar.R, na.rm = TRUE)
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(df)))
p <- numeric(nrow(df))
for (k in 1:5) {
fit <- ranger(Ozone ~ Solar.R + Wind + Temp + Month, df[fold != k, ], num.trees = 300, seed = 1)
p[fold == k] <- predict(fit, df[fold == k, ])$predictions
}
e <- df$Ozone - p
c(rmse = sqrt(mean(e^2)), mae = mean(abs(e)), r2 = 1 - sum(e^2) / sum((df$Ozone - mean(df$Ozone))^2))Recap
Recap- MAE is the typical miss, and RMSE punishes big ones.
- R² compares squared error with a mean-only baseline, and it can be negative on new rows.
- Choose the loss and metric from what the decision needs. Avoid MAPE near zero.
Concepts in this lesson