Cross-Validation interview question

Machine LearningCross-ValidationClass ImbalanceData Leakagemedium

A telecom churn model (3% churners) is evaluated with the code below and reports a mean CV recall of 0.99. On customers that were set aside before any resampling, recall at the same threshold is 0.31. What is the main cause, and what is the fix?

python
from imblearn.over_sampling import RandomOverSampler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold, cross_val_score

ros = RandomOverSampler(random_state=0)
X_res, y_res = ros.fit_resample(X, y)        # churners: 3% -> 50%

cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
scores = cross_val_score(RandomForestClassifier(), X_res, y_res,
                         cv=cv, scoring="recall")
print(scores.mean())                          # 0.99
  1. ARecall is the wrong metric after oversampling, since resampling shifts the decision threshold; keep the code as it is and report ROC-AUC instead, which is threshold-free.
  2. Bshuffle=True mixes the oversampled rows across folds; with shuffle=False each fold keeps a contiguous block of rows, so copies stay out of the validation folds.
  3. CRandom oversampling only duplicates rows; switching to SMOTE before cross_val_score creates new synthetic churners, so no validation row also appears in training.
  4. DCopies of each churner land in both training and validation folds, so the forest is scored on rows it memorised; resample in an imblearn Pipeline on training folds only.
Show the answer and explanation

Correct answer: D, Copies of each churner land in both training and validation folds, so the forest is scored on rows it memorised; resample in an imblearn Pipeline on training folds only.

Explanation Cross-Validation

RandomOverSampler duplicates minority rows until the classes are balanced. Going from 3% to 50% churners means each churner appears about 0.97 / 0.03 ≈ 32 times on average.

Because resampling ran before the split, cross_val_score scatters these copies across all five folds: almost every churner in a validation fold has identical copies in the training folds. A random forest reproduces its training rows almost perfectly, so validation recall is close to 1.0. It measures memorisation, not generalisation, and the untouched holdout shows the real level.

Fix: make the sampler part of the model, so it runs inside each training fold only.

from imblearn.pipeline import make_pipeline
pipe = make_pipeline(RandomOverSampler(random_state=0), RandomForestClassifier())
cross_val_score(pipe, X, y, cv=cv, scoring="recall")

An imblearn Pipeline calls fit_resample only during fit and skips the sampler at prediction time, so validation folds keep their real 3% churn rate. A plain scikit-learn Pipeline rejects samplers, because they have no transform method.

Why the other fixes fail: SMOTE before CV leaks too, since synthetic churners are interpolated from validation rows and their neighbours. Fold shuffling is not the cause; duplicates would still be split across folds. And switching the metric leaves the leak in place, because any metric is inflated when validation rows were memorised.

Interview tip: say "resampling, scaling, imputation, encoding and feature selection are part of the model, so they go inside the CV loop", and mention class weights as a simpler alternative to oversampling.

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 Machine Learning questions

  1. An insurer has 3,000 claims confirmed as fraud and 120,000 claims nobody has investigated. A calibrated classifier trained to separate confirmed (s = 1) from…
  2. A helpdesk routes tickets with a KNN classifier using Euclidean distance on TF-IDF vectors built with norm=None (no length normalisation). Tickets range from…
  3. For a declined credit-card application, an Anchors explainer returns the rule IF utilization > 0.80 AND missed_payments_12m ≥ 2 THEN decline, with precision…
  4. A payments company's fraud team reports F1 and ROC-AUC, yet trains its classifier with gradient descent on log loss. A product manager asks: "Why not minimise…
  5. A food-delivery app ranks restaurants with a Wide & Deep model: the wide part is a linear model on crossed features such as user_last_cuisine ×…