A regression model predicts numbers: prices, sales, delivery times. Its predictions are never exactly right, so the question is how wrong, on average, and in what way? A single number that answers this is called a metric. You need it to compare models, to tune them, and to tell your manager whether the model is good enough to use.
There are several standard metrics, and they answer slightly different questions. Below: MAE, MSE, RMSE, R² and MAPE, each computed by hand and with scikit-learn, how outliers change them, and a guide to choosing one.
What you need first: linear regression explained and loss functions explained. A loss is what the model minimises while training; a metric is what you report afterwards, and they're often the same formula.
Key terms
- Error (or residual): actual value minus predicted value, for one row.
- MAE (mean absolute error): the average size of the errors, ignoring their sign.
- MSE (mean squared error): the average of the squared errors.
- RMSE (root mean squared error): the square root of MSE, back in the target's units.
- R² (R-squared): the share of the variation in the target that the model explains, compared with always predicting the average.
- MAPE (mean absolute percentage error): the average error as a percentage of the actual value.
- Baseline: a deliberately simple model, such as "always predict the average", that a real model must beat.
Regression metrics on one example
A model predicts delivery times, in minutes, for six orders:
import numpy as np
actual = np.array([30, 45, 25, 60, 40, 35])
predicted = np.array([32, 41, 27, 52, 43, 35])
errors = actual - predicted
print(errors)
[-2 4 -2 8 -3 0]
Each error (or residual) is actual minus predicted. Some are positive, some negative, so simply averaging them would let them cancel out: the mean error here is 0.83 minutes, which hides that the model was 8 minutes off once. Every metric below fixes this in its own way.
Keep the tall +8 bar in mind as you read on: the metrics differ mainly in how much they let that one big miss count.
MAE: mean absolute error
Take the absolute value of each error (drop the minus signs), then average:
MAE = mean(|actual − predicted|)
Read the bars |…| as "the size of", ignoring the sign: |−2| is 2.
mae = np.abs(errors).mean()
print(round(mae, 2))
3.17
"On average, predictions are about 3.2 minutes off." MAE is in the same units as the target and is the easiest metric to explain to anyone. Every minute of error counts the same.
MSE: mean squared error
Square each error, then average:
MSE = mean((actual − predicted)²)
mse = (errors ** 2).mean()
print(round(mse, 2))
16.17
Squaring makes errors positive and punishes big errors much more: the 8-minute error contributes 64, while the 2-minute errors contribute only 4 each. MSE is what linear regression minimises, and it's mathematically convenient. Its drawback: the units are squared (minutes²), which means nothing to a person.
RMSE: root mean squared error
Take the square root of MSE to get back to the original units:
RMSE = √MSE
Predict first: MAE was 3.17 minutes. Will RMSE be larger, smaller or the same? Think about what squaring does to the 8-minute error.
rmse = np.sqrt(mse)
print(round(rmse, 2))
4.02
Also in minutes, like MAE, but larger (4.02 vs 3.17), because the one big error weighs more. RMSE is always at least as large as MAE. A big gap between them means a few large errors.
R²: how much is explained
R² (R-squared) compares the model with the simplest possible "model": always predicting the average.
R² = 1 − (sum of squared errors of the model) / (sum of squared errors of predicting the mean)
ss_model = (errors ** 2).sum()
ss_mean = ((actual - actual.mean()) ** 2).sum()
r2 = 1 - ss_model / ss_mean
print(round(r2, 3))
0.874
| R² | Meaning |
|---|---|
| 1 | Perfect predictions |
| 0.874 | The model explains about 87% of the variation in delivery times |
| 0 | No better than always predicting the average |
| Below 0 | Worse than predicting the average: something is badly wrong |
R² has no units, so it's handy for comparing models on different targets. But "good" depends on the field: 0.6 can be excellent for predicting human behaviour and poor for a physics experiment. And R² doesn't tell you how big the errors are; always report it alongside MAE or RMSE.
MAPE: mean absolute percentage error
MAPE expresses errors as a percentage of the actual value:
mape = (np.abs(errors) / actual).mean() * 100
print(f"{mape:.1f}%")
7.4%
"Predictions are off by about 7% on average." Percentages are easy for business people, and they let you compare across scales: 5 minutes off a 10-minute delivery is worse than 5 minutes off a 60-minute one. But MAPE breaks when actual values are zero or near zero (division by zero, or huge percentages), so avoid it for targets like daily sales of rarely bought items.
The same with scikit-learn
from sklearn.metrics import (mean_absolute_error, mean_squared_error,
root_mean_squared_error, r2_score,
mean_absolute_percentage_error)
print(round(mean_absolute_error(actual, predicted), 2))
print(round(mean_squared_error(actual, predicted), 2))
print(round(root_mean_squared_error(actual, predicted), 2))
print(round(r2_score(actual, predicted), 3))
print(round(mean_absolute_percentage_error(actual, predicted), 3))
3.17
16.17
4.02
0.874
0.074
The order of arguments is always (actual, predicted); for most metrics swapping them changes nothing, but for R² and MAPE it does. root_mean_squared_error needs scikit-learn 1.4 or newer; on older versions use mean_squared_error(...) ** 0.5. Scikit-learn's MAPE is a fraction (0.074), not a percentage.
How outliers change the metrics
Suppose one order goes badly wrong: actual 120 minutes, because the delivery driver got lost, but the model predicted 40.
actual_2 = np.append(actual, 120)
predicted_2 = np.append(predicted, 40)
print("MAE ", round(mean_absolute_error(actual_2, predicted_2), 2))
print("RMSE", round(root_mean_squared_error(actual_2, predicted_2), 2))
MAE 14.14
RMSE 30.47
MAE rose from 3.2 to 14.1, but RMSE jumped from 4.0 to 30.5, because the 80-minute error is squared. That's neither good nor bad; it depends on what you care about:
- If big errors are especially costly (a late medicine delivery, a badly underpriced loan), use RMSE, which punishes them.
- If every unit of error costs the same, or your data has outliers you don't want to dominate, use MAE.
Always compare with a baseline
A metric means little on its own. Is an MAE of 3.2 minutes good? Compare it with a simple baseline: always predicting the average.
baseline = np.full_like(actual, actual.mean(), dtype=float)
print(round(mean_absolute_error(actual, baseline), 2))
9.17
The model's MAE of 3.2 is about three times better than the baseline's 9.2: a genuinely useful model. If a model can't beat the baseline clearly, it isn't worth deploying. Scikit-learn's DummyRegressor builds baselines for you; see baseline models.
Experiment: is the average really the best constant to predict? Try every whole number of minutes from 20 to 60 as a constant prediction, and find the one with the lowest MAE and the one with the lowest RMSE. Predict whether both will pick the mean (39.2).
constants = np.arange(20, 61)
best_mae = constants[np.argmin([mean_absolute_error(actual, np.full(6, c)) for c in constants])]
best_rmse = constants[np.argmin([root_mean_squared_error(actual, np.full(6, c)) for c in constants])]
print("lowest MAE at", best_mae, " lowest RMSE at", best_rmse)
print("median", np.median(actual), " mean", round(actual.mean(), 1))
lowest MAE at 35 lowest RMSE at 39
median 37.5 mean 39.2
They disagree. RMSE is lowest at 39, the whole number nearest the mean: squared error is always minimised by the mean. MAE prefers a lower value. With six orders, any constant between the 3rd and 4th smallest times (35 and 40) gives exactly the same MAE, 9.17 minutes, and argmin reports the first, 35; the median, 37.5, sits in the middle of that flat stretch. MAE is minimised by the median. So the metric you choose quietly decides what "the best guess" means: the mean is pulled up by the slow 60-minute order, the median isn't.
And always measure metrics on a test set the model didn't train on, as explained in train, validation and test sets. Training-set metrics are flattering.
Which metric should you use?
| Metric | Units | Outliers | Use when |
|---|---|---|---|
| MAE | Same as target | Robust | You want an easy-to-explain typical error; all errors cost the same |
| MSE | Squared | Very sensitive | Training models (it's what many minimise); rarely for reporting |
| RMSE | Same as target | Sensitive | Large errors are especially bad; the most common reported metric |
| R² | None (0 to 1, can be negative) | Sensitive | Comparing how much is explained; always alongside MAE or RMSE |
| MAPE | Percent | Breaks near zero | Business reporting, comparing across scales, targets never near zero |
A good habit: report one error metric in real units (MAE or RMSE) plus R², both on the test set, plus the baseline for comparison.
Practice
- Actual values are
[10, 20, 30]and predictions are[12, 18, 33]. Compute MAE and RMSE by hand.
Show the answer
The errors are −2, 2 and −3. MAE = (2 + 2 + 3) / 3 ≈ 2.33. MSE = (4 + 4 + 9) / 3 ≈ 5.67, so RMSE ≈ 2.38.
- Model A has MAE 5 and RMSE 6. Model B has MAE 4 and RMSE 12. What's different about their errors?
Show the answer
Model B is usually more accurate (lower MAE) but makes some large errors (RMSE much larger than MAE). Model A's errors are more consistent. Which is better depends on how costly big errors are.
- A model's R² on test data is −0.3. What does that mean?
Show the answer
It predicts worse than simply always guessing the average. It may be badly overfitted, trained on different data, or have a bug such as mismatched rows.
- Check your answer to question 1 with scikit-learn.
Show the answer
a, p = [10, 20, 30], [12, 18, 33]
print(round(mean_absolute_error(a, p), 2), round(root_mean_squared_error(a, p), 2))
2.33 2.38
- A taxi company says its fare-prediction model has an RMSE of ₹40, measured on the data it was trained on. Give two reasons you can't yet tell whether the model is good.
Show the answer
First, it's measured on training data, which flatters the model; it needs a test set. Second, there's no baseline: if always predicting the average fare already gives an RMSE of ₹45, the model is barely helping, but if the baseline is ₹200, it's excellent. (A third: ₹40 means something different for ₹100 fares than for ₹2,000 fares, so MAPE or R² alongside it would help.)
Interview questions
What is the difference between MAE and RMSE?
Both are in the target's units. MAE averages absolute errors, treating all errors equally and being robust to outliers. RMSE squares errors before averaging, so it penalises large errors more. RMSE ≥ MAE, and a large gap indicates some big errors.
What does R² measure, and can it be negative?
The proportion of variance in the target explained by the model, relative to predicting the mean. It can be negative on new data when the model does worse than predicting the mean.
When should you not use MAPE?
When actual values can be zero or close to zero, because the percentage error becomes undefined or explodes. It also penalises over- and under-predictions asymmetrically.
Key takeaways
- A raw average of errors hides mistakes, because positive and negative errors cancel.
- MAE is the typical error in real units; RMSE is too, but punishes big errors more, so RMSE ≥ MAE.
- R² compares the model with always predicting the average; it has no units and can be negative.
- MAPE gives a percentage but breaks when actual values are near zero.
- Report a real-units metric plus R², on a test set, next to a baseline.
Frequently asked questions
What is a good RMSE?
There's no universal value: RMSE is in the target's units, so judge it against the baseline's RMSE and against what errors cost in practice. An RMSE of 4 minutes is excellent for 60-minute deliveries and poor for 5-minute ones.
Should I use MAE or RMSE?
Use RMSE when large errors are disproportionately costly, MAE when every unit of error costs the same or the data has outliers. Many teams report both: the gap between them shows whether a few big errors exist.
Why is my R² negative?
On a test set, R² is negative when the model's errors are larger than those of predicting the test set's average. Common causes are severe overfitting, a bug such as misaligned rows, or test data that differs from the training data.
What to learn next
- Find the score every model must beat in baseline models, the next lesson.
- Put it all together in the house price prediction project.
- See how a metric is minimised during training in loss functions explained.