Sub topic 13
Deciding when a model is good enough
This chapter assumes you can fit scikit-learn models, know accuracy, recall and mean absolute error, and understand standard error. It also assumes you have already built a baseline and scored it with a fixed metric on fixed splits.
The working model
A model is good enough when it meets a requirement written down before you looked at results. "Better than the baseline" is a necessary condition, not the decision. The decision combines four inputs:
- Gain over baseline. The improvement must be larger than run-to-run noise.
- Requirement. The product sets a minimum useful gain or a target score.
- Ceiling. Human performance or irreducible error limits how much more is possible.
- Cost. Complexity, latency and maintenance must be worth the gain.
Fix the criterion in advance
Write the criterion before the final evaluation. Take the minimum useful gain from the product, for example "catch 5 more fraud cases per 100 without raising false alarms", and not from the data. If you choose the threshold after seeing scores, you will tend to choose one the model already passes.
A rough check in code
The script below scores a baseline and a model on identical folds, then tests the paired gain against the threshold. Both calls reuse one seeded cv object, so fold i is the same split for both models.
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
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 lower bound subtracts two standard errors from the mean gain. That makes the test conservative about noise, but only roughly. Repeated cross-validation folds share training data, so the standard error is optimistic. Treat the result as a guide, and be more cautious when the lower bound is close to the threshold.
Use the ceiling
If humans label the same items with 90% agreement, a model at 88% has little room left. If the baseline is at 50% and the ceiling is 90%, a model at 70% has closed half the gap. Reporting "fraction of the gap closed" often communicates more than a raw score. Where no human estimate exists, a very flexible model on cleaner data can give a rough bound.
Common errors
- Choosing the threshold after the results. Fix it first.
- Judging on the average only. A model can pass overall and fail on a slice that matters. Check the slices from your error analysis against the same criterion.
- Comparing on different splits. Gains then mix model quality with split luck.
- Ignoring cost. A 0.5-point gain that doubles serving latency may not be worth shipping.
- Treating "good enough" as permanent. Data drifts, so record the criterion and revisit it after deployment.
Exercise
A model's mean gain over the baseline is 0.08 with a standard error of 0.02. The minimum useful gain is 0.05. Is the rough criterion from this chapter met, and what caveat applies?
Show answer
The lower bound is 0.08 - 2 × 0.02 = 0.04, which is below 0.05, so the criterion is not met. The standard error from overlapping cross-validation folds is also optimistic, so the true margin is likely smaller still.