A colleague reports a cross-validated AUC of 0.91 with this code on 200 customers and 10,000 features. As a sanity check you rerun it with randomly permuted labels and still get an AUC of about 0.80. What is the problem?
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
# X: 200 rows x 10,000 features, y: binary label
selector = SelectKBest(f_classif, k=50).fit(X, y)
X_sel = selector.transform(X)
scores = cross_val_score(LogisticRegression(max_iter=1000),
X_sel, y, cv=5, scoring="roc_auc")
print(scores.mean()) # 0.91Show the answer and explanation
Correct answer: D, The selector saw the labels of every row, including each validation fold, so it picked features that fit noise there; refit SelectKBest inside each fold via a Pipeline.
Explanation Feature Selection
This is selection bias (a form of leakage), famously described for gene-expression studies. With 10,000 features and only 200 rows, some features correlate with any label vector by pure chance, including random labels. SelectKBest was fitted on all rows, so the 50 "best" features were chosen partly because they match the labels of the rows that later act as validation folds. Cross-validation then evaluates on data that already influenced the model.
The fix is to treat selection as part of the model and refit it inside every training fold:
pipe = make_pipeline(SelectKBest(f_classif, k=50),
LogisticRegression(max_iter=1000))
cross_val_score(pipe, X, y, cv=5, scoring="roc_auc")With permuted labels this pipeline gives AUC ≈ 0.5, as it should. If you also tune k, use nested CV.
Why the alternatives fail: swapping the scoring function to mutual information keeps the same leak. Scaling changes how the model is fitted, not the fact that validation labels were used in selection. Leave-one-out still evaluates features chosen with the held-out row's label, so the bias remains, and a one-row validation fold cannot even produce an AUC.
Interview tip: the permuted-label test is a great habit to mention. Any step that looks at y (selection, target encoding, resampling, tuning) must live inside the CV loop.