Skip to content

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.

Applied ~11 min9 steps

What runs in CodeSplash

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

01

Five rows

Read

Suppose a model's errors on five rows are 1, 1, 1, 1 and 16.

MAE=1+1+1+1+165=4RMSE=1+1+1+1+2565≈7.2\text{MAE} = \frac{1+1+1+1+16}{5} = 4 \qquad \text{RMSE} = \sqrt{\frac{1+1+1+1+256}{5}} \approx 7.2

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.

02

Which metric moved?

Predict

Predict first

You fix that one row's error, so 16 becomes 1. Which metric improves by the larger factor?

MAE moves moreRMSE moves moreThey move the same
Reveal

RMSE, from about 7.27.2 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.

03

R² is a comparison

Read

R²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:

R2=1−∑(yi−y^i)2∑(yi−yˉ)2R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2}

For ozone, baseline RMSE 34.6 and linear RMSE 22.9 give roughly 1−(22.9/34.6)2≈0.561 - (22.9/34.6)^2 \approx 0.56. A forest, at 19.0, takes that to about 0.700.70.

On held-out rows, R2R^2 can go below zero: the model did worse than predicting the mean.

04

Look at the errors

Explore

Points on the diagonal are perfect. Look for a pattern in the misses. Do they grow with the prediction? Does the model flatten the extremes?

Predicted vs actualHow close are a regression model's predictions to the true values, and where does it miss?
View data
sale price (k€)predicted price (k€)split
291.40289.60test
174.30132.80validation
361.60381validation
189.50261.60validation
415.20381.80validation
253.70322.20test
412.90366.50validation
392.10368.20validation
425344.20validation
373.40372.50validation
93.300140.10test
140.80162.90validation
131.60181.40validation
428.10376.50validation
71.80089.800validation
301214.80test
413.30371.40validation
364.70337.70validation
287.30357.30validation
160.30190.50validation
392.40415.20test
273.60319.70validation
156.40236.10validation
107.90120.90validation
11386.700validation
49.900102.70test
391.90392.80validation
251.60242.80validation
141.10189.90validation
141233.20validation
177.20200.10test
237.80274.70validation
205.10304.60validation
300.10345.50validation
371.20404.80validation
410.10362.30test
385.90341.50validation
231.70192.40validation
230.60208.50validation
336.90302.10validation
366.10328.20test
222281.10validation
390.50370.10validation
327.60310validation
423.30393validation
301.70306.50test
329.70317.20validation
460.10325.50validation
248.70214.70validation
194.90209validation
243.70215test
398.30412.80validation
176.70179.80validation
182.30148validation
240.30288.10validation
263.40323test
425.50396.80validation
98.90097.800validation
371.80345.70validation
95.300109.50validation

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.

05

Match the loss to the question

Read

Squared 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 0.80.8 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.

06

Percentages

Predict

Predict 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?

About 1%About 50%Exactly 100%
Reveal

About 50%: (100%+0.1%)/2(100\% + 0.1\%)/2. 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.

07

Check your understanding

Checkpoint

Check yourself

Four errors: 3, 4, 0 and 0. What is the RMSE?

Show the answer

Answer: 2.5

(9+16+0+0)/4=6.25(9 + 16 + 0 + 0)/4 = 6.25, and 6.25=2.5\sqrt{6.25} = 2.5. The MAE is 1.751.75. Identical errors would make the two equal.

Check yourself

A model has a held-out R2R^2 of −0.2-0.2. 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.

08

Choose your metric

In the app

In 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.

Python
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))
R
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))
09

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