Sub topic 10
Choosing and reporting a metric
This chapter assumes you can compute residuals, MAE, RMSE, and R squared, and that you know why they must be measured on held-out data. Here you will decide which metric to optimize and how to report results so others can trust them.
Start from the cost of an error
No metric is best in general. Each one encodes an opinion about which mistakes matter. Ask two questions first:
- Is one large miss much worse than several small ones?
- Does your audience need the answer in the target's units?
If a forecast that is off by 10 units is more than twice as bad as one off by 5, the cost grows faster than linearly. That favors RMSE. If every unit of error costs about the same, as with a delivery estimate where each extra minute annoys a customer equally, MAE matches the cost better.
What each metric is good for
| Metric | Units | Strength | Weakness |
|---|---|---|---|
| MAE | Target units | Easy to explain. Resistant to a few extreme points. | Treats a miss of 10 as exactly twice a miss of 5. |
| RMSE | Target units | Punishes large errors. Common in competitions and papers. | One outlier can dominate the score. |
| R squared | None | Compares models on different scales against the mean baseline. | Says nothing about error size in real units. Can mislead (see the previous chapter). |
RMSE is never smaller than MAE on the same errors. If the two are close, errors are fairly even. If RMSE is much larger, a few big misses are driving it. Reporting both shows this spread at no extra cost.
Optimize one metric, report several
Choose one primary metric before you compare models. Otherwise you can drift toward whichever number looks best. Then report a small set around it:
- The primary metric in target units (MAE or RMSE).
- R squared, so readers can judge the model against the mean baseline.
- The baseline's own score, so the number has a reference point.
- The size and origin of the evaluation set, which must be held-out data.
An RMSE of 12.0 means little alone. It means a lot next to a baseline RMSE of 55.0.
A reporting example
The code below scores a mean baseline and a linear model on the same test split, then prints a compact report.
import numpy as np
from sklearn.datasets import make_regression
from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
X, y = make_regression(n_samples=200, n_features=3, noise=10.0, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
models = {
"mean baseline": DummyRegressor(strategy="mean"),
"linear model": LinearRegression(),
}
print(f"Test set size: {len(y_test)}")
for name, model in models.items():
pred = model.fit(X_train, y_train).predict(X_test)
mae = mean_absolute_error(y_test, pred)
rmse = np.sqrt(mean_squared_error(y_test, pred))
r2 = r2_score(y_test, pred)
print(f"{name:14s} MAE={mae:7.2f} RMSE={rmse:7.2f} R2={r2:6.3f}")The baseline row should show an R squared near 0. It can be slightly negative, because the training mean is not exactly the test mean. The linear model row should show much lower errors. The gap between the rows is the improvement your model actually delivers.
Common reporting mistakes
- Reporting training scores as results. They are optimistic. Use the test set, or cross-validation.
- Switching metrics after seeing results. Fix the metric first.
- Comparing RMSE across different targets. An RMSE of 5 on house prices in thousands and an RMSE of 5 on temperature are not comparable. Use R squared or scale-free measures for that.
- Omitting units. Write "RMSE of 4.2 minutes", not "RMSE of 4.2".
- Ignoring the baseline. A good-looking number can still be barely better than predicting the mean.
Summary
Pick the primary metric from the cost of errors. Use RMSE when large misses are especially bad and MAE when costs scale evenly. Add R squared and a mean baseline for context, state the units, and compute everything on held-out data.
Exercise
A food-delivery team predicts delivery time in minutes. Customers get upset in proportion to the minutes late, and a few orders have wildly wrong times because of data entry mistakes. Which primary metric would you choose, and what else would you report alongside it?
Show answer
Choose MAE. Cost grows evenly with each minute, and MAE is less distorted by the few bad data points than RMSE. Alongside it, report RMSE to show whether large misses exist, R squared, and the mean baseline's MAE, all computed on a held-out test set and stated in minutes.