Sub topic 13
Measuring whether a feature helps
This chapter assumes you know scikit-learn's fit and predict workflow, the idea of a train/test split, and the pipeline from the previous chapter. The question here is how to tell whether a new feature improves a model or only seems to.
Why a single score is not enough
A feature change is an experiment. If you fit on all your data and score on the same data, almost any extra feature looks useful, because the model can memorize noise. A single train/test split is better, but the score depends on which rows landed in the test set. A small gain can disappear with a different split.
Cross-validation (CV) splits the training data into k folds. It trains on k-1 folds, scores on the held-out fold, and repeats so each fold is held out once. You get k scores, and their spread tells you how much to trust a difference.
Keep preprocessing inside each fold
Imputers, scalers, encoders and selectors learn from data. If you fit them on all rows before CV, information from the validation fold leaks into training, and the scores come out too high. Pass the whole pipeline to the CV function. Scikit-learn then refits every step on each training fold.
from sklearn.model_selection import cross_val_score
# model is the Pipeline from the previous chapter
# X is a DataFrame with the columns above, y is the target
scores = cross_val_score(model, X, y, cv=5, scoring="roc_auc")
print(scores.mean(), scores.std())The mean is your estimate of performance. The standard deviation shows fold-to-fold noise.
Compare a baseline and a candidate
Change one thing at a time. Score a baseline pipeline, add the feature, and score again with the same folds. Reusing the same splits makes the comparison fair, because both models see identical train and validation rows. Fix the splits with a KFold or StratifiedKFold object that has a random_state.
from sklearn.model_selection import StratifiedKFold, cross_val_score
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
base = cross_val_score(model, X, y, cv=cv, scoring="roc_auc")
cand = cross_val_score(model_with_new_feature, X, y, cv=cv, scoring="roc_auc")
diff = cand - base
print("mean gain:", diff.mean(), "std of gain:", diff.std())Here model_with_new_feature is the same pipeline with your new feature added. Looking at the per-fold differences is more informative than comparing two means.
Reading the result
| Observation | Likely meaning |
|---|---|
| Gain is much larger than the fold spread, and positive in most folds | The feature probably helps |
| Gain is smaller than the spread | Treat it as noise. Prefer the simpler model |
| Training score rises but CV score does not | The feature adds overfitting |
| A very large jump | Check for leakage before celebrating |
Common errors
- Fitting transformers before CV. This is the leakage described above. Fix it by putting every learned step in the pipeline.
- Choosing a metric that hides the problem. Accuracy on imbalanced classes can stay flat while ROC AUC or recall changes. Pick the metric that matches the goal.
- Tuning on the test set. Try many feature ideas with CV on the training data. Touch the held-out test set once, at the end.
- Trying too many ideas. If you test fifty features against the same folds, some will look good by chance. Keep a small set of hypotheses, and confirm winners on fresh data.
- Ignoring structure. For time-ordered data, use
TimeSeriesSplit. For repeated rows from one customer, useGroupKFold. Random folds would put related rows on both sides.
Exercise
You add a feature and the mean 5-fold ROC AUC rises from 0.810 to 0.815, while the fold-to-fold standard deviation is 0.020. Should you keep the feature? Name one step that would make your decision firmer.
Show answer
The gain of 0.005 is much smaller than the spread of 0.020, so it is probably noise, and the simpler model is preferable. To firm up the decision, compute the per-fold differences on identical folds, or use repeated CV (for example RepeatedStratifiedKFold) to get more scores and a tighter estimate.