Explore Library
Code Quiz

Cross-Validation Data Leakage Bug

Spot the preprocessing mistake that leaks test-fold information during cross-validation.

Codepython
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score

# Fit scaler and evaluate a regularized model with 5-fold CV
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

model = Ridge(alpha=1.0)
scores = cross_val_score(model, X_scaled, y, cv=5, scoring='r2')

print('Mean CV R2:', scores.mean())

What is the bug that makes the reported CV score overly optimistic?