Sub topic 6
A Baseline to Beat
This chapter assumes you can load the California housing data into a DataFrame, have seen the error metrics MAE, RMSE and R², and know why we hold out a test set. Here you build the simplest possible model and record its error. Every later model has to beat that number.
Why a baseline comes first
A score has no meaning on its own. Suppose a model reports a mean absolute error (MAE) of 0.53. The target, MedHouseVal, is in units of $100,000, so that is about $53,000 of average error. Whether that is good depends on what a trivial guess would score.
A baseline is a model that uses no real learning. It sets a floor. If a complex model barely beats the floor, the extra complexity is not paying for itself. If it fails to beat the floor, something is wrong, often in the code.
Split the data, then fit the baseline
The split uses a fixed random_state, so you get the same rows every run. DummyRegressor(strategy="mean") ignores the features and predicts the mean of the training targets for every row.
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyRegressor
from sklearn.metrics import mean_absolute_error
df = fetch_california_housing(as_frame=True).frame
X = df.drop(columns="MedHouseVal")
y = df["MedHouseVal"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
baseline = DummyRegressor(strategy="mean").fit(X_train, y_train)
print("Baseline MAE:", mean_absolute_error(y_test, baseline.predict(X_test)))The printed MAE is roughly 0.9, which is about $90,000 of error per house. Your exact digits may differ slightly. That is the number to beat.
What the baseline tells you
The mean predictor always gives the same value, so its R² is close to 0 on held-out data. R² measures how much better a model is than predicting the mean, so the baseline defines the zero point of that scale. A model with R² of 0.8 explains much of the variation that the mean ignores.
The baseline also exposes problems early:
- If a real model scores worse than the baseline, check for a bug in feature handling or in the split.
- If a real model scores suspiciously close to perfect, suspect leakage, which the next chapter covers.
- If the baseline is already good enough for the business need, you may not need a model at all.
Other baseline strategies
DummyRegressor has other strategies. Which one is fair depends on the metric you care about.
| Strategy | Predicts | Best matched to |
|---|---|---|
| `"mean"` | Training mean | RMSE and R² |
| `"median"` | Training median | MAE |
The median minimizes absolute error, so it is the tougher baseline when you report MAE. Using it makes your comparison stricter.
How the baseline fits the workflow
Common errors
- Fitting the baseline on all the data. This lets test targets influence the mean.
- Comparing scores from different splits or metrics. Use the same split and the same metric throughout.
- Forgetting the units. Report MAE in dollars, here by multiplying by 100,000.
Exercise
Change the baseline to strategy="median" and compare its test MAE with the mean baseline. Which is lower, and why would you expect that?
Show answer
Use DummyRegressor(strategy="median"). Its MAE should be slightly lower, because the median minimizes absolute error while the mean minimizes squared error. House values are skewed, so the two differ noticeably.
Exercise
A new model has a test MAE of 0.95 against a baseline MAE of 0.90. What does this suggest, and what should you check first?
Show answer
The model is worse than guessing a constant, which signals a bug or a poor setup. Check that features and target are aligned, that the model is fitted on the training data, and that the same split and metric are used for both.