Measuring regressors
Last updated
Lesson 2.8 scored classifiers, whose answers are right or wrong. A regressor's answers are numbers, and every one is off by something. How far off, on average? Are a few predictions wildly wrong? Is the model better than just guessing the average? This lesson answers all three with MAE, RMSE and R², and shows how to read a model's mistakes from a residual plot.
By the end of this lesson you can
- Compute MAE, RMSE and R² by hand and with scikit-learn, and say what each means
- Compare a regressor against the mean as a baseline
- Read a residual plot for patterns the model has missed
- Explain why a high R² on training data says little
Errors in the units of y
A classifier is right or wrong. A regressor that predicts 4,100 g for a penguin of 4,200 g isn't "wrong"; it's 100 g off. So regression metrics start from the residual of each example (lesson 3.1):
the truth minus the prediction. Here's a straight line (lesson 3.1) predicting body mass from flipper length, trained on 70% of the penguins and tested on the other 103:
On the test penguins, the predictions are off by 309 g on average, and the second number, 391 g, is another way of averaging the same errors. They're both in grams, which is the first thing to like about them: you can say them to someone who's never heard of machine learning.
MAE and RMSE
The two averages:
MAE, the mean absolute error, is the plain average size of a miss. RMSE, the root mean squared error, squares each miss first (lesson 2.7's squared error), averages, then takes the square root to get back to grams. Squaring makes big misses count for more, so RMSE is always at least MAE, and the gap between them says how uneven the errors are. Here, 391 against 309: a few penguins are missed by a lot more than the typical 300 g.
Which to report? MAE if every gram of error costs the same; RMSE if a big miss is much worse than two small ones, which is often true (a price estimate that's ₱2 million off loses the sale). And RMSE is what a model trained on squared error, like linear regression, is trying to make small.
scikit-learn has both:
R²: better than the mean?
Is 391 g good? It depends on how much penguins vary in the first place. The simplest possible "model" predicts the same number for everyone: the mean mass of the training penguins. That's the baseline every regressor must beat, as always predicting the commonest class is for a classifier (lesson 2.8).
Predicting the mean misses by 772 g (RMSE). The line's 391 g is about half that. R², the coefficient of determination, puts that comparison in one number:
the squared error of the model, divided by the squared error of predicting the mean, taken away from 1. So:
- : perfect predictions.
- : no better than the mean. The
DummyRegressorscores about 0 (−0.002 here, since it uses the training penguins' mean on the test penguins). - : worse than the mean. It can happen, and it's a loud warning.
The line's R² is 0.742: it explains about three quarters of the variation in mass. Move the line yourself and watch all three numbers:
MAE 475 g, RMSE 573 g, R² 0.488. A slope of 49.7 g per mm is the best. Look at the residuals on the right: a slope in them means the line is missing something.
Residual plots
One number can't say where a model goes wrong. Plot the residuals against the prediction (or against a feature):
A good model leaves residuals that look like noise: a shapeless band around 0, about as wide everywhere. Things to look for:
- A curve or a slope in the residuals: the model is missing a shape in the data. Try another feature, or a model that can bend.
- A funnel, narrow at one end and wide at the other: the errors grow with the prediction. Predicting the log of often helps (lesson 4.1).
- A few far-off points: outliers, or data entry mistakes, worth looking at one by one (lesson 4.12).
Here the band is shapeless, but wide: penguins with the same flippers differ in mass by hundreds of grams, for reasons flipper length can't see, such as sex and the time of year. A better model needs more information, not just a more flexible line.
Wrong way: R² on the training data
Fit 15 penguins with a straight line, a cubic, and a degree-12 polynomial (lesson 3.8), and score each on its own 15 penguins and on the test set:
The degree-12 curve's R² on its training penguins is 0.996, nearly perfect. On penguins it hasn't seen it's −150: a hundred and fifty times worse than predicting the mean. The training R² rose as the model got more flexible, and it always will, because a more flexible model can bend closer to the points it was fitted to (lesson 2.6).
So the rule from lesson 2.5 holds for regression too: report scores on data the model hasn't seen. A training R² only tells you the model could fit the data, not that it learned anything.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- scikit-learn: Regression metrics: mean_absolute_error, root_mean_squared_error and r2_score
- scikit-learn: DummyRegressor: predicting the mean, as a baseline
- Chai and Draxler, Root mean square error (RMSE) or mean absolute error (MAE)?, Geoscientific Model Development 7(3), 2014: what each of the two measures is best for