Sub topic 2
Baselines: Know What to Beat
Assume you know Python, pandas, supervised learning, and scikit-learn’s fit and predict interface. This chapter uses binary classification and ROC AUC, the area under the receiver operating characteristic curve. Use a different metric when your application requires one.
Start with a baseline
A baseline is a simple reference model. For classification, a majority-class or prior-probability predictor often provides a useful lower bound. A complex model is valuable only if it improves on that reference under the same validation procedure.
Linear and logistic models
Logistic regression estimates a probability from a weighted sum of features. It is fast, often interpretable, and a strong first model when the decision boundary is close to linear. Standardize numeric features when their scales differ.
Decision trees and their limits
A decision tree repeatedly splits rows by feature values. It can represent nonlinear rules and feature interactions without scaling. A deep tree can memorize training examples, so control its depth, minimum leaf size, or pruning settings.
Random forests
A random forest trains many trees on different samples and feature subsets, then averages their predictions. This bagging approach usually reduces the variance of one tree. It handles nonlinear relationships well, but can use more memory and is less transparent than a single tree.
Gradient-boosted trees
Gradient boosting builds trees sequentially. Each new tree focuses on errors left by the current ensemble. It often performs strongly on tabular data, but learning rate, tree size, and number of trees need careful tuning.
| Model | Main strength | Common risk |
|---|---|---|
| Logistic regression | Fast and interpretable | Misses nonlinear patterns |
| Random forest | Stable nonlinear predictions | Larger, less interpretable models |
| Gradient boosting | Strong tabular accuracy | Overfitting or tuning complexity |
from sklearn.datasets import load_breast_cancer
from sklearn.dummy import DummyClassifier
from sklearn.ensemble import HistGradientBoostingClassifier, RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
models = {
"baseline": DummyClassifier(strategy="prior"),
"logistic": make_pipeline(StandardScaler(), LogisticRegression(max_iter=2000)),
"tree": DecisionTreeClassifier(max_depth=4, random_state=0),
"forest": RandomForestClassifier(n_estimators=200, random_state=0),
"boosting": HistGradientBoostingClassifier(random_state=0),
}
for name, model in models.items():
scores = cross_val_score(model, X, y, cv=cv, scoring="roc_auc")
print(f"{name}: {scores.mean():.3f} +/- {scores.std():.3f}")Exercise
Why must every candidate model be compared with a baseline on the same folds?
Show answer
The baseline shows whether the added complexity produces a real improvement under the same data conditions.