Sub topic 3
MSE and RMSE: Squaring Errors, Then Returning to Original Units
This chapter assumes you can use NumPy arrays, know what a mean and a square root are, and have seen residuals (prediction minus true value) from the previous chapters. Here you will compute Mean Squared Error (MSE) and Root Mean Squared Error (RMSE), and learn the mistakes people commonly make with them.
Why square the errors
Residuals can be positive or negative, so averaging them directly lets misses cancel. A model that is off by +5 on one row and -5 on another has a mean residual of 0, which hides the problem. MAE avoids this with absolute values. MSE avoids it by squaring each residual first, since a square is never negative.
The definition:
- MSE = mean of (prediction - true value)²
- RMSE = square root of MSE
Squaring also changes the units. If the target is in dollars, MSE is in dollars squared, which is hard to interpret. Taking the square root turns the result back into dollars. RMSE is therefore the value you normally report.
A worked example
Use the same four points as before. The true values are 3.0, 5.0, 2.5 and 7.0. The predictions are 2.5, 5.0, 4.0 and 8.0.
| True | Predicted | Residual | Squared residual |
|---|---|---|---|
| 3.0 | 2.5 | -0.5 | 0.25 |
| 5.0 | 5.0 | 0.0 | 0.00 |
| 2.5 | 4.0 | 1.5 | 2.25 |
| 7.0 | 8.0 | 1.0 | 1.00 |
The squared residuals sum to 3.5. Dividing by 4 rows gives MSE = 0.875. The square root of 0.875 is about 0.9354, so RMSE ≈ 0.9354.
Compare this with MAE, which is 0.75 for the same data. RMSE is larger. That is not a coincidence, and the next chapter explains why.
Computing it in NumPy
import numpy as np
def mse(y_true, y_pred):
residuals = np.asarray(y_pred) - np.asarray(y_true)
return np.mean(residuals ** 2)
def rmse(y_true, y_pred):
return np.sqrt(mse(y_true, y_pred))
y_true = [3.0, 5.0, 2.5, 7.0]
y_pred = [2.5, 5.0, 4.0, 8.0]
print(f"MSE = {mse(y_true, y_pred):.4f}") # 0.8750
print(f"RMSE = {rmse(y_true, y_pred):.4f}") # 0.9354Converting the inputs with np.asarray lets the function accept plain lists. Without it, y_pred - y_true raises a TypeError on lists.
Reading the numbers
RMSE of 0.9354 means that a typical prediction misses by roughly 0.94 target units. "Roughly" matters here. RMSE is not the average miss. It is the square root of the average squared miss, so it is always greater than or equal to MAE. The two are equal only when every error has the same size.
MSE and RMSE are both zero for a perfect model and grow as predictions get worse. Lower is better. Neither has an upper bound, and neither is meaningful on its own scale. An RMSE of 0.94 is excellent for house prices in millions of dollars and terrible for a target that ranges from 0 to 1. Judge it against the spread of the target.
Common errors
- Reporting MSE as if it were in original units. If the target is in meters, MSE is in square meters. Report RMSE, or label the units clearly.
- Taking the square root too early. The RMSE is the root of the mean of squares. It is not the mean of the roots, which would just be MAE again. Square, average, then root, in that order.
- Using the sum instead of the mean. The sum of squared residuals (SSE) grows with the number of rows. MSE divides by the row count, so scores from test sets of different sizes remain comparable.
- Averaging the residuals before squaring. The mean of the residuals here is 0.5. Squaring that gives 0.25, which is not the MSE. Square each residual first.
- Relying on old scikit-learn arguments.
mean_squared_error(..., squared=False)was removed in recent versions. Usenp.sqrt(mean_squared_error(...)), which works in every version, orroot_mean_squared_errorin scikit-learn 1.4 and later.
Check your understanding
Exercise
A model predicts [10, 20, 30] for true values [12, 18, 33]. Compute the MSE and the RMSE. Then explain why the RMSE is the more useful number to report if the target is measured in kilograms.
Show answer
The residuals are -2, 2 and -3. Their squares are 4, 4 and 9, which sum to 17. MSE = 17 / 3 ≈ 5.667 kg². RMSE = sqrt(5.667) ≈ 2.380 kg. RMSE is more useful because it is in kilograms, the same unit as the target, so it reads as a typical miss size. MSE is in squared kilograms and has no direct physical meaning.