Sub topic 15
A repeatable selection workflow
This chapter assumes you know the earlier steps: framing the problem, choosing a metric, building leakage-safe cross-validation, writing pipelines, and fitting baselines and the main model families. Here you will combine them into one procedure that you can rerun on any new tabular problem.
Why a fixed procedure helps
Model choice goes wrong in predictable ways. You may tune one model harder than the others. You may switch metrics after seeing results. You may pick the winner on the same data that tuned it. A written procedure removes these options, and it lets a colleague reproduce your decision later.
The workflow
- Freeze the setup. Write down the target, the metric, and the folds before you fit anything. Keep a final holdout set if you have enough data, and do not touch it until step 6.
- Build one preprocessing pipeline. Fit it inside each fold so imputing and scaling never see validation rows.
- Score the baseline. Every later model must beat it by more than the fold-to-fold noise.
- Compare untuned candidates. Use a linear model, a random forest, and gradient boosting. Add a neural network only if the data is large or has structure that suits it.
- Tune only the leaders. Use nested cross-validation, so the reported score covers the whole tuning procedure.
- Apply practical constraints. Check interpretability, prediction latency, and maintenance cost. Then confirm on the holdout once and record the decision.
A reusable comparison table
This snippet scores each candidate on identical folds and records score, spread, and training time. It is self-contained, so you can run it as shown.
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.dummy import DummyClassifier
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_validate
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = make_classification(n_samples=500, n_features=8, random_state=0)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
candidates = {
"baseline": DummyClassifier(strategy="prior"),
"logreg": make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)),
"hist_gb": HistGradientBoostingClassifier(random_state=0),
}
rows = []
for name, model in candidates.items():
res = cross_validate(model, X, y, cv=cv, scoring="roc_auc")
rows.append({
"model": name,
"auc_mean": res["test_score"].mean(),
"auc_std": res["test_score"].std(),
"fit_seconds": res["fit_time"].mean(),
})
print(pd.DataFrame(rows).sort_values("auc_mean", ascending=False).round(3))Keep this table in your project notes. When a new model arrives, add a row and rerun it on the same folds.
Reading the result
Compare the gap between two models with the standard deviation across folds. If two models differ by less than one standard deviation, treat them as tied. Then let the constraints decide, usually in favor of the simpler or faster model.
Common errors
- Changing the metric or folds midway. Scores from different setups are not comparable.
- Reporting the tuning score as the final score. The best score from a search is optimistically biased. Report the nested or holdout score instead.
- Tuning every model, then picking the best. Each extra search gives that model more chances to look good by luck.
- Skipping the write-up. Without a record of data version, folds, metric, and seeds, you cannot repeat the decision.
Exercise
Two tuned models score 0.912 and 0.915 AUC, each with a fold standard deviation of 0.010. The first is logistic regression with a 2 ms prediction time. The second is gradient boosting with an 8 ms prediction time. Which do you choose, and why?
Show answer
Choose logistic regression. The 0.003 gap is well inside the 0.010 fold noise, so the models are effectively tied. Logistic regression is easier to interpret and faster to serve, so the practical constraints decide.