Sub topic 8
Telling a real improvement from noise
This chapter assumes you know how to score a model with cross-validation (CV, which splits the data into folds and rotates which fold is held out). It also assumes you know the mean, standard deviation and standard error, and basic scikit-learn use. The previous chapters covered baselines and the splitting rules.
Why one score is not enough
A model score depends on which rows landed in the test fold, on random seeds, and on the sample size. If you train the same model twice with different splits, you get two different numbers. A candidate that scores 0.02 higher than the baseline in one run may be no better at all.
So a comparison needs two things: a spread for each score, and a way to compare the models on equal terms.
Score both models on identical folds
Repeated cross-validation runs k-fold CV several times with different shuffles. This gives many scores per model, and their spread estimates the run-to-run noise. Pass the same seeded cv object to both models so that fold i contains the same rows for each.
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"
)
print("baseline:", baseline_scores.mean(), baseline_scores.std())
print("logreg: ", model_scores.mean(), model_scores.std())
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)The code subtracts the scores fold by fold. This is a paired comparison. Folds that are hard for one model tend to be hard for the other, so the differences vary less than the raw scores do.
Reading the result
The last lines apply a simple rule. The mean gain minus two standard errors is a rough lower bound on the improvement. If that bound is still above the minimum useful gain, the improvement is probably real and large enough to matter.
Fix min_useful_gain before you look at the results. If you choose it afterwards, you can pick a value that makes any result pass.
Common errors
- Comparing models on different splits. Differences then mix model quality with split luck.
- Reporting only the mean. Always report the spread too.
- Leaving the seed unset. Results cannot be reproduced, and the folds may differ between the two calls.
- Tuning the candidate on the same folds you use for the final comparison. This inflates its score. Keep a separate test set for the final check.
- Calling any positive gain a win. A gain below the noise level or below the minimum useful gain is not a reason to ship.
Exercise
A candidate beats the baseline by a mean of 0.03 balanced accuracy, with a standard error of 0.01 over the paired folds. The minimum useful gain was fixed at 0.05. Is the candidate good enough on this criterion?
Show answer
No. The lower bound is 0.03 - 2 * 0.01 = 0.01, which is below 0.05. The gain looks real, because the bound is above zero. But it is not shown to be large enough to matter, so keep iterating or gather more evidence.