Scaled Before Split

This script reports 0.93 from 5-fold cross-validation. A month later the same model scores 0.71 on freshly collected data.

from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectKBest
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score

X = SelectKBest(k=20).fit_transform(X_raw, y)   # 1
X = StandardScaler().fit_transform(X)           # 2
scores = cross_val_score(SVC(), X, y, cv=5)     # 3
print(scores.mean())                            # 0.93

Which single change makes the reported number honest?

A

Move only line 1 inside the loop. Line 2 never sees y, so scaling on the full dataset cannot leak anything.

B

Raise cv from 5 to 10 so each held-out fold is smaller and the average is less pessimistic.

C

Hand cross_val_score a make_pipeline(SelectKBest(k=20), StandardScaler(), SVC()) and the raw X_raw, so both steps are refit inside every fold.

D

Swap the default splitter for StratifiedKFold so the class balance of each fold matches the full dataset.