Feature Selection interview question

Preprocessing & Feature EngineeringFeature SelectionCross-ValidationData Leakagehard

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?

python
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.91
  1. AThe F-test assumes normally distributed features, and with 10,000 skewed features its scores are inflated; replacing f_classif with mutual_info_classif in the same line fixes it.
  2. BLogistic regression on 50 unscaled features overfits each training fold; adding a StandardScaler before the model would bring the permuted-label AUC back down to 0.5.
  3. CFive folds are too few for 200 rows, so each model trains on too little data; leave-one-out cross-validation would remove the optimistic bias from the AUC estimate.
  4. DThe 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.
Show 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.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Preprocessing & Feature Engineering questions

  1. For an offline consumption report (not a forecasting feature), an analyst fills gaps in a building's daily energy readings. The readings were logged on…
  2. A card-fraud dataset was extracted on 30 June. A transaction's label is 1 if a chargeback had arrived by the extraction date, and chargebacks can arrive up to…
  3. A retailer's click model has 200M impressions, 2M of them clicks. To cut training cost, you keep every click and 1 in 10 non-clicks, and give each kept…
  4. A bank vectorizes 2 million support tickets with TF-IDF over a 500,000-term vocabulary; the sparse matrix has about 100 non-zeros per row (≈ 2.4 GB in CSR…
  5. A retailer's store_id has 1,000 levels. Instead of one-hot encoding, a colleague proposes binary encoding: give each store an integer code and write that code…