Sub topic 4
Trivial baselines: the floor to beat
This chapter assumes you know supervised learning basics, common metrics such as accuracy and mean absolute error (MAE), and scikit-learn. A trivial baseline is a predictor that ignores the input features, or uses almost none of them. It sets the floor: a model that cannot beat it has learned nothing useful.
Why start with a trivial predictor
A trivial baseline costs minutes to build and has no tuning to argue about. It also exposes problems early. A broken pipeline, a leaky split, or a poorly chosen metric often shows up as a "good" score that a constant prediction matches.
Fit every baseline on the training split only and score it on the held-out split with the same metric you will use for your model.
Choosing the trivial predictor by task
Match the constant to the metric. The mean minimizes squared error, and the median minimizes absolute error. Using the median as the baseline for an MAE comparison gives you the hardest constant to beat.
Classification: majority class
On imbalanced data, always predicting the common class scores high accuracy. Balanced accuracy, the average of per-class recall, exposes this.
from sklearn.datasets import make_classification
from sklearn.dummy import DummyClassifier
from sklearn.metrics import accuracy_score, balanced_accuracy_score
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=2000, weights=[0.95, 0.05], random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=0
)
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
pred = baseline.predict(X_test)
print("accuracy:", accuracy_score(y_test, pred))
print("balanced accuracy:", balanced_accuracy_score(y_test, pred))Accuracy comes out near 0.95 and balanced accuracy is exactly 0.5. A model with 94% accuracy on this data is worse than doing nothing, even though 94% sounds strong.
Regression: mean or median
A constant regressor predicts one training-set statistic for every row. The next example compares a median baseline with a linear regression on the same split.
import numpy as np
from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
X = rng.normal(size=(500, 3))
y = 3.0 * X[:, 0] - 2.0 * X[:, 1] + rng.normal(scale=1.0, size=500)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
for name, model in [
("median", DummyRegressor(strategy="median")),
("linear", LinearRegression()),
]:
model.fit(X_train, y_train)
mae = mean_absolute_error(y_test, model.predict(X_test))
print(name, round(mae, 3))The median baseline gives a high MAE. The linear model, a first simple-model baseline, should score far lower. The gap between the two tells you how much signal the features carry.
Forecasting: the naive forecast
For time series, predict that the next value equals the last observed value. Any forecasting model must beat this error.
import numpy as np
# Daily values; the naive forecast predicts that tomorrow equals today.
y = np.array([102.0, 105.0, 103.0, 108.0, 110.0, 109.0, 113.0])
actual = y[1:]
naive_forecast = y[:-1]
mae = np.mean(np.abs(actual - naive_forecast))
print("naive MAE:", mae)This prints 2.833. Persistence is hard to beat when the series moves slowly, so a model that loses to it is not forecasting anything.
Common errors
- Computing the constant from the test set. Use training data only, or you leak information.
- Comparing metrics across different splits. Baseline and candidate must share the same data and the same metric.
- Using a random split for time series. Split by time, so the baseline never sees the future.
- Treating a beaten baseline as proof of quality. Beating the floor shows only that the model is not useless. Later chapters cover how much improvement is enough.
Exercise
A fraud dataset has 99% legitimate transactions. A model reports 98.5% accuracy. A majority-class baseline reports 99%. What do you conclude, and which metric would you switch to?
Show answer
The model is worse than the trivial baseline on accuracy, so it adds nothing as measured. Accuracy is the wrong metric here. Switch to balanced accuracy, or to precision and recall on the fraud class, and compare the model with the baseline again on the same split.