Sub topic 9
Comparing a candidate to the baseline
This chapter assumes you can fit scikit-learn models, know accuracy, balanced accuracy and mean absolute error, and understand standard deviation and standard error. It also assumes you have already chosen a metric and built a trivial baseline in earlier chapters.
What makes a comparison fair
A candidate model is only better than the baseline if the two were scored under identical conditions. Check four things before reading any numbers:
- Both models use the same metric, chosen before you looked at results.
- Both models are evaluated on the same data splits.
- Preprocessing is fitted on training folds only, so no test information leaks in.
- The random seeds are fixed, so the run can be repeated.
If any of these differ, a gap between the scores may come from the setup and not from the model.
Scoring both models on the same folds
Passing one seeded cross-validation object to every call gives each model the same folds. The code below uses an imbalanced classification problem, so it scores with balanced accuracy.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RepeatedStratifiedKFold, cross_val_score
X, y = make_classification(n_samples=2000, weights=[0.95, 0.05], random_state=0)
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=0)
baseline_scores = cross_val_score(
DummyClassifier(strategy="prior"), X, y, cv=cv, scoring="balanced_accuracy"
)
model_scores = cross_val_score(
LogisticRegression(max_iter=1000), X, y, cv=cv, scoring="balanced_accuracy"
)
diff = model_scores - baseline_scores
print("baseline:", baseline_scores.mean(), baseline_scores.std())
print("logreg: ", model_scores.mean(), model_scores.std())
print("paired gain per fold:", np.round(diff, 3))Because both arrays follow the same fold order, model_scores[i] - baseline_scores[i] is a paired difference on one fold. Pairing removes the variation that comes from some folds being harder than others.
Reading the gain against noise
Report the mean of the paired differences and a rough noise allowance. The standard error is the standard deviation of the differences divided by the square root of their count.
import numpy as np
# Reuses baseline_scores and model_scores from the previous example.
diff = model_scores - baseline_scores
mean_gain = diff.mean()
std_err = diff.std(ddof=1) / np.sqrt(len(diff))
min_useful_gain = 0.05 # set from the product requirement, not from the data
lower_bound = mean_gain - 2 * std_err
print("mean gain:", mean_gain)
print("rough lower bound:", lower_bound)
print("good enough on this criterion:", lower_bound > min_useful_gain)Set min_useful_gain from the product need before you see the results. If you choose it afterward, the test only confirms what you already believe.
Common errors
- Comparing against a weaker setup. The baseline should get the same features, the same preprocessing and a fair amount of tuning effort.
- Reading one split. A single train/test split gives one noisy number. A gain smaller than the fold-to-fold spread is not evidence.
- Tuning on the comparison folds. If you pick hyperparameters using the same folds you report, the candidate's score is optimistic. Use nested cross-validation or a separate held-out test set.
- Reporting only the mean. Give the spread or the paired differences too, so a reader can see how stable the gain is.
- Ignoring the metric's meaning. A gain of 0.05 in balanced accuracy is not the same as a gain of 0.05 in accuracy. Convert it into a business effect, such as missed positives per thousand cases.
Exercise
Two models have fold scores with a standard deviation of about 0.03. Model B's mean is 0.01 higher than model A's, and the folds were not paired. Is that evidence that B is better? What would you change?
Show answer
No. The 0.01 gap is smaller than the 0.03 fold-to-fold spread, so it could be noise. Score both models on identical folds from one seeded cross-validation object, compute the paired difference per fold, and check whether the mean gain minus about two standard errors still exceeds a minimum useful gain fixed in advance. Treat that bound as optimistic because the folds overlap.
Exercise
Why must min_useful_gain come from the product requirement and not from the observed results?
Show answer
A threshold chosen after seeing the scores can be set just below the observed gain, so the check always passes. A threshold fixed in advance from what the product needs makes the check a real test of whether the model is worth shipping.