Sub topic 5
The predict-the-mean baseline
This chapter assumes you can read basic NumPy code, know what a mean is, and understand that a regression model predicts a numeric target. It also assumes you have seen the residual, MAE and RMSE definitions from the earlier chapters. R squared is built on the baseline described here, so the next chapter depends on it.
Why a metric needs a reference point
Suppose a model has an RMSE of 12. Is that good? The number has no meaning until you compare it with something. If the target values typically vary by 100 units, an RMSE of 12 is strong. If they vary by 13 units, it is barely better than guessing.
A baseline gives you that reference. The simplest baseline needs no features and no training. It ignores the inputs and predicts the same number for every row: the mean of the target values.
What the baseline predicts
The predict-the-mean baseline answers every question with the average of y_true. If the average house price is 300,000, the baseline says 300,000 for every house. It is the best single constant you can pick if you want to minimize squared error. That fact is why squared error is the natural yardstick here.
Any model worth using should beat this baseline. A model that does worse has learned nothing useful beyond the average, and it may have learned something harmful.
Measuring the baseline's error
The baseline's total squared error has a name: the total sum of squares, written ss_tot. It is the sum of squared distances between each true value and the mean of the true values.
import numpy as np
y_true = np.array([3.0, 5.0, 2.5, 7.0])
baseline = np.full_like(y_true, y_true.mean())
print("baseline prediction:", baseline)
print("deviations:", y_true - baseline)
ss_tot = np.sum((y_true - y_true.mean()) ** 2)
print("ss_tot:", ss_tot)The mean of these four values is 4.375. The deviations are -1.375, 0.625, -1.875 and 2.625. Squaring and summing them gives ss_tot = 12.6875.
This number is the same as the variance of the target multiplied by the number of rows. In other words, ss_tot measures how spread out the target is. A target that barely varies has a small ss_tot, and a mean-only guess is already hard to beat.
Comparing a model with the baseline
A model has its own total squared error, called the residual sum of squares, ss_res. It is the sum of squared residuals from the model's predictions.
Both quantities are on the same scale, so you can compare them directly. The ratio ss_res / ss_tot tells you what fraction of the baseline's error remains after using the model.
import numpy as np
y_true = np.array([3.0, 5.0, 2.5, 7.0])
y_pred = np.array([2.5, 5.0, 4.0, 8.0])
ss_res = np.sum((y_true - y_pred) ** 2)
ss_tot = np.sum((y_true - y_true.mean()) ** 2)
print("ss_res:", ss_res)
print("ss_tot:", ss_tot)
print("remaining error ratio:", ss_res / ss_tot)Here ss_res is 3.5 and ss_tot is 12.6875. The ratio is about 0.276. The model keeps roughly 28% of the baseline's squared error, so it removes about 72%. That 72% figure is the R squared value you will define formally in the next chapter.
The baseline scores exactly zero
If you feed the baseline's own predictions into this comparison, ss_res equals ss_tot. The ratio is 1, and the improvement is 0. This is true by construction, for any data.
import numpy as np
y_true = np.array([3.0, 5.0, 2.5, 7.0])
y_pred = np.full_like(y_true, y_true.mean())
ss_res = np.sum((y_true - y_pred) ** 2)
ss_tot = np.sum((y_true - y_true.mean()) ** 2)
print(1 - ss_res / ss_tot) # 0.0So zero means "no better than predicting the mean". Anything above zero means the model beats the baseline. Anything below zero means it is worse.
Common errors
- Using the wrong mean. The baseline mean comes from the data you are scoring. On a test set, a baseline built from the test mean is a slightly optimistic reference, because it uses information a real predictor would not have. Many practitioners instead use the training mean. The two usually differ only a little, but be consistent and say which one you used.
- Comparing errors with different units.
ss_resandss_totare squared quantities. Do not comparess_reswith an MAE or an RMSE. Compare like with like. - Treating the baseline as a weak straw man. On noisy targets, the mean can be hard to beat. A model that barely improves on it is a real finding, not a failure of the metric.
Exercise
Exercise
A target has values [10, 12, 14, 16]. Compute the predict-the-mean baseline's predictions and sstot. A model predicts [11, 12, 15, 15]. Compute ssres and the ratio ssres / sstot. Does the model beat the baseline?
Show answer
The mean is 13, so the baseline predicts 13 for every row. The deviations are -3, -1, 1, 3, so sstot = 9 + 1 + 1 + 9 = 20. The model's residuals are -1, 0, -1, 1 in magnitude, so ssres = 1 + 0 + 1 + 1 = 3. The ratio is 3 / 20 = 0.15. The model keeps 15% of the baseline's squared error, so it beats the baseline, with an improvement of 0.85.
Exercise
Why does the predict-the-mean baseline always score exactly 0 on the comparison 1 - ssres / sstot when scored on the same data it took its mean from?
Show answer
Its predictions equal the mean of ytrue, so ssres is computed from the same deviations as ss_tot. The two sums are identical, the ratio is 1, and 1 - 1 = 0.