Sub topic 5
Simple models as strong baselines
This chapter assumes you can fit scikit-learn estimators, know the metrics from the previous chapters, and have a held-out split ready. Trivial baselines such as the majority class or the median set the floor. A simple model sets a much harder bar, and it is the one a complex model has to beat to justify itself.
Why a simple model comes next
A constant predictor ignores the features. A simple model uses them in the most direct way available. If a linear model already captures most of the signal, a gradient-boosted ensemble may add little beyond cost and maintenance burden. If the simple model fails badly, you learn that the problem has structure it cannot express, which is a useful finding.
Good candidates
| Task | Simple baseline | What it tells you |
|---|---|---|
| Classification | Logistic regression | How much signal is roughly linear |
| Regression | Linear or ridge regression | Same, for a numeric target |
| Either | Decision tree, depth 2 to 5 | Whether a few interactions or thresholds explain the target |
| Forecasting | Seasonal naive, or linear model on lag features | Whether the series has more than persistence |
Keep the settings close to defaults. The point is a fair reference, not a tuned contender. If you tune the baseline heavily, it stops being cheap and quick to rebuild.
Building them
This example compares the majority class, a scaled logistic regression and a shallow tree on the imbalanced data from earlier chapters. All three use the same split and the same metric.
from sklearn.datasets import make_classification
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import balanced_accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
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
)
models = {
"majority": DummyClassifier(strategy="most_frequent"),
"logreg": make_pipeline(
StandardScaler(),
LogisticRegression(max_iter=1000, class_weight="balanced"),
),
"tree depth 3": DecisionTreeClassifier(
max_depth=3, class_weight="balanced", random_state=0
),
}
for name, model in models.items():
model.fit(X_train, y_train)
score = balanced_accuracy_score(y_test, model.predict(X_test))
print(name, round(score, 3))The majority predictor scores 0.5 on balanced accuracy. The other two show how much the features add on top of that floor.
Common errors
- Scaling outside the pipeline. Fitting
StandardScaleron all the data before splitting leaks test statistics into training. Put preprocessing inside aPipelineso it is fitted on training data only. - Forgetting to scale for linear models. Unscaled features slow convergence and make regularization uneven across features.
- Ignoring imbalance. Without
class_weightor a suitable decision threshold, logistic regression on 5% positives may rarely predict the positive class. Judge it on the metric you chose, not on accuracy. - Letting the tree grow. An unrestricted tree memorizes the training set. A shallow depth keeps it a baseline and keeps it readable.
- Comparing on different splits. A baseline scored on one split and a candidate on another gives a difference that mixes model quality with split luck.
Reading the result
The gap between the simple model and the trivial baseline shows how much signal the features carry. The gap between the simple model and your candidate shows what complexity buys. If the second gap is small, you may stop there. If the simple model scores close to a ceiling you estimated, further modelling has little room to help.
Exercise
Your gradient-boosted model scores 0.81 balanced accuracy and a logistic regression scores 0.79 on the same split. The boosted model takes ten times longer to train and is harder to explain. What should you check before choosing it?
Show answer
Check whether the 0.02 gap exceeds run-to-run noise by using repeated cross-validation on identical folds and comparing the paired differences. Then check whether the gain meets the minimum useful gain set from the product requirement. If it does not, the simple model is the better choice given its lower cost and easier maintenance.