Regression metrics: MAE, RMSE, R2 and MAPE

What MAE, RMSE, R2 and MAPE each reward and punish, how to compute them by hand, and how to choose the one that matches the cost of a wrong prediction.

Lesson 34 of 6610 minBeginner

You will learn to

  • Compute MAE, RMSE, R2 and MAPE
  • Explain why RMSE punishes large errors
  • Avoid MAPE traps near zero

Before this lesson

Intuition

A regression model predicts a number, so each prediction is wrong by some amount. A metric turns the list of errors into one score. The different metrics answer different questions about the same list.

Think of a delivery company that estimates how many minutes each trip will take. MAE asks: "On average, how many minutes are we off?" Every minute of error counts the same. RMSE asks the same question but reacts strongly to big misses: one trip that is 60 minutes late hurts much more than six trips that are 10 minutes late. R2 asks: "How much better are we than a lazy guess that always says the average duration?" MAPE asks: "By what percentage are we off?", which helps when a 5-minute error on a 10-minute trip is worse than a 5-minute error on a 3-hour trip.

None of them is the best in general. The right one depends on what a wrong prediction costs you.

Picture it

The key difference between MAE and RMSE is how the penalty grows with the size of one error.

01234Size of one errorPenaltyAbsolute error (MAE)Squared error (MSE, RMSE)
One error of size 4 costs 4 under absolute error but 16 under squared error. RMSE is the square root of the average squared error.

Absolute error grows in a straight line. Squared error grows like a bowl: small errors almost disappear, large ones dominate.

The math

Let yiy_i be the true value, y^i\hat{y}_i the prediction and nn the number of rows. Then:

MAE=1n∑i=1n∣yi−y^i∣RMSE=1n∑i=1n(yi−y^i)2\text{MAE} = \frac{1}{n}\sum_{i=1}^{n} |y_i - \hat{y}_i| \qquad \text{RMSE} = \sqrt{\frac{1}{n}\sum_{i=1}^{n} (y_i - \hat{y}_i)^2}
R2=1−∑i(yi−y^i)2∑i(yi−yˉ)2MAPE=1n∑i=1n∣yi−y^iyi∣R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2} \qquad \text{MAPE} = \frac{1}{n}\sum_{i=1}^{n} \left|\frac{y_i - \hat{y}_i}{y_i}\right|

Here yˉ\bar{y} is the mean of the true values. MSE is RMSE before the square root. Some facts follow:

  • RMSE is always at least as large as MAE. They are equal only when every error has the same size. The bigger the gap, the more uneven the errors are.
  • MAE and RMSE are in the unit of the target (euros, minutes). MSE is in squared units, which is why people prefer RMSE.
  • In R2, the top sum is what the model gets wrong and the bottom sum is what the mean-only baseline gets wrong. R2 = 1 is perfect, R2 = 0 matches the baseline, and R2 below 0 is worse than the baseline.
  • MAPE is a fraction (multiply by 100 for percent). It is undefined when a true value is 0.

Worked example

Five houses, prices in thousands. The numbers are made up to keep the arithmetic easy.

HouseTruePredictedError (predicted minus true)
1200210+10
2250240-10
3300310+10
4350330-20
5400450+50
  • MAE = (10 + 10 + 10 + 20 + 50) / 5 = 100 / 5 = 20.
  • MSE = (100 + 100 + 100 + 400 + 2500) / 5 = 3200 / 5 = 640, so RMSE = sqrt(640) = 25.3.
  • R2: the mean true price is 300, so the baseline error sum is 100² + 50² + 0 + 50² + 100² = 25,000. R2 = 1 - 3200 / 25,000 = 0.872.
  • MAPE = (0.05 + 0.04 + 0.033 + 0.057 + 0.125) / 5 = 0.305 / 5 = 6.1 percent.

House 5 alone contributes 2500 of the 3200 squared error. If its error were only +10, then MAE would be 12 and RMSE would be 12.6: almost equal. The gap between MAE (20) and RMSE (25.3) in the table is the sign that one prediction is far off. Look at that row before trusting the average.

How mlflow does it

For regression runs, the leaderboard shows MAE, MSE, RMSE, R2, RMSLE and MAPE for every model. In all of them lower is better, except R2, where higher is better; the leaderboard and the model page say which direction applies to the metric you look at. Click a column header to sort by it. Scores on the leaderboard are cross-validated averages. See the metrics reference in the docs for the full list.

Common mistakes

  • Judging a number without a baseline. An RMSE of 25 means nothing until you know the spread of the target. Compare with the baseline model on the leaderboard, or look at R2.
  • Using RMSE when outliers are noise. A few corrupted rows can dominate it, and you end up tuning the model to chase bad data. Check the largest errors first and consider MAE.
  • Using MAE when big misses are costly. A model with rare disasters looks as good as a steady one. Pick RMSE when one large error is much worse than several small ones.
  • Using MAPE with zeros or tiny values. The percentage explodes or is undefined. Use MAE, or another scale-free error, for targets that touch zero.
  • Reading R2 as accuracy. An R2 of 0.9 can still mean unacceptable errors in euros, and a low R2 can be fine when the target is noisy. Always translate the error back to the unit you care about.
  • Comparing metrics across different targets. RMSE on prices and RMSE on log prices are not comparable. Compare models on the same target.

Try it in mlflow

Sort a regression leaderboard

  1. Open Runs and open a finished regression run.
  2. On the leaderboard, click the RMSE column header to sort by it, then click the MAE header.
  3. Check whether the same model stays on top. If the order changes, the two metrics disagree about how much a large error matters.
  4. Open a model candidate and read its primary metric: it says whether lower or higher is better.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. Two models have the same MAE. Model A makes many small errors. Model B makes a few very large errors and many tiny ones. Which has the higher RMSE?
  2. 2. A model predicts house prices and gets R2 = 0.00 on validation data. What does this mean?
  3. 3. Why is MAPE a poor choice when some true values are close to zero?