Sub topic 10
Setting a Baseline
This chapter assumes you can split data with scikit-learn, build a Pipeline, and read a metric such as accuracy or F1. A baseline is a deliberately simple model that gives you a reference score. A trained model only counts as useful if it beats that reference.
Why a baseline matters
A score means little without context. Suppose a model reaches 94% accuracy. If 90% of rows belong to one class, a model that always predicts that class already scores 90%. Your model then adds four points, not 94.
A baseline answers three questions:
- Is the problem learnable from these features at all?
- How much does a complex model improve on a trivial rule?
- Is the pipeline working? A model that scores below its baseline usually has a bug.
Kinds of baselines
| Baseline | What it does | Use it when |
|---|---|---|
| Majority class | Always predicts the most frequent class | Classification |
| Mean or median | Always predicts the training mean or median | Regression |
| Simple rule | Applies a hand-written rule, such as "flag orders above a set amount" | You have domain knowledge |
| Simple model | A small logistic regression or a shallow tree | You want a fair first comparison |
| Current process | The score of the existing system or human decision | You are replacing something |
Pick a baseline that matches your metric from the framing step. If success is measured by F1, compare F1 scores, not accuracy.
A majority-class baseline in code
DummyClassifier ignores the features and follows a fixed strategy. Fit it on training rows only, then score it on the same data as your real model.
from sklearn.datasets import load_breast_cancer
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42
)
baseline = DummyClassifier(strategy="most_frequent")
model = Pipeline([
("scale", StandardScaler()),
("clf", LogisticRegression(max_iter=1000)),
])
for name, estimator in [("baseline", baseline), ("logistic", model)]:
scores = cross_val_score(estimator, X_train, y_train, cv=5, scoring="f1")
print(name, round(scores.mean(), 3))The comparison uses cross-validation on the training data. The test set stays untouched until the final evaluation.
Common errors
- Comparing different metrics or splits. Score the baseline and the model with the same metric, the same folds, and the same data.
- Fitting the baseline on test data. This is leakage, even for a trivial model.
- Using accuracy on imbalanced data. A majority-class baseline looks strong on accuracy and scores zero on the minority-class F1. Report a metric that exposes this.
- Stopping at the dummy. Beating a majority-class rule is a low bar. Add a simple model, such as logistic regression, so later complex models face a fair comparison.
Exercise
A fraud dataset has 99% legitimate transactions. A model scores 99.2% accuracy. Why is this result weak evidence, and what would you check?
Show answer
A majority-class baseline that labels everything legitimate already scores 99% accuracy, so the model adds only 0.2 points. Compare against that baseline using a metric that reflects the rare class, such as recall, precision, or F1 on the fraud class, with the same cross-validation folds for both.