Sub topic 2
Why RMSE Reacts More Strongly to Large Errors Than MAE
Assume you know basic NumPy operations, averages, square roots, and the purpose of training and test data. This chapter compares error metrics by their formulas and behavior.
Why RMSE Reacts More Strongly to Large Errors Than MAE
A residual is the prediction error:
\[ ei = \hat{y}i - y_i \]
Its sign shows direction. A negative residual means the model predicted too low. A positive residual means it predicted too high.
Mean Absolute Error (MAE) averages the absolute residuals:
\[ MAE = \frac{1}{n}\sum |e_i| \]
Root Mean Squared Error (RMSE) squares residuals, averages them, and takes the square root:
\[ RMSE = \sqrt{\frac{1}{n}\sum e_i^2} \]
Squaring makes large errors count disproportionately. Both metrics use the target's units, but RMSE is more affected by outliers.
| Errors | MAE | RMSE |
|---|---|---|
| 1, 1, 1, 1 | 1 | 1 |
| 0, 0, 0, 4 | 1 | 2 |
Both error sets have MAE 1. The second set has a larger RMSE because of its single large miss.
The Predict-the-Mean Baseline
A baseline is a simple reference model. In regression, the predict-the-mean baseline always predicts the mean of the target values. It gives a useful standard for judging whether a model learned anything beyond a constant guess.
R Squared: How Much Better Than the Baseline?
R squared, written \(R^2\), compares a model's squared errors with the baseline's squared errors:
\[ R^2 = 1 - \frac{\sum(yi-\hat{y}i)^2} {\sum(y_i-\bar{y})^2} \]
An \(R^2\) of 1 is perfect. An \(R^2\) of 0 matches the mean baseline. A negative value means the model is worse than that baseline, which can happen on held-out test data. R squared does not show the prediction error in target units, so report it with MAE or RMSE.
Computing the Metrics with scikit-learn
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
y_true = [3.0, 5.0, 2.5, 7.0]
y_pred = [2.5, 5.0, 4.0, 8.0]
mae = mean_absolute_error(y_true, y_pred)
rmse = np.sqrt(mean_squared_error(y_true, y_pred))
r2 = r2_score(y_true, y_pred)
print(f"MAE={mae:.4f} RMSE={rmse:.4f} R2={r2:.4f}")Exercise
Which metric is more affected by one unusually large residual, MAE or RMSE, and why?
Show answer
RMSE, because it squares residuals before averaging.