Explore Library
Code QuizAdvanced

Data Leakage in Model Evaluation

Spot why scaling the whole dataset before splitting inflates your validation score.

Codepython
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)  # scale first

X_train, X_test, y_train, y_test = train_test_split(
    X_scaled, y, test_size=0.2, random_state=42
)

model.fit(X_train, y_train)
print('Test accuracy:', model.score(X_test, y_test))

What is the bug that makes the reported test accuracy unreliable?