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?
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.99Show 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.