Every preprocessing bug traces back to one mistake — learning from the test set. Split before you transform anything, and understand exactly what data leakage is.
Why: if any information from the test set influences training — even the mean used to fill a missing value — your evaluation is a lie and the model fails in production. When: this is the single most important rule in all of ML engineering. Where: leakage makes offline metrics look great and real-world performance collapse.
LEAKAGE = test-set information sneaks into training.
WRONG: scale the WHOLE dataset, THEN split
-> the scaler saw test rows -> test metric is inflated
RIGHT: split FIRST, fit the scaler on TRAIN only,
then APPLY it to test
-> test set stays truly unseen
If your offline score is suspiciously high, suspect leakage first.Why: the split must be the very first step so no later transformation can peek at the test set. When: split immediately after loading, before imputing, scaling, or encoding. Where: set random_state for reproducibility and stratify for classification so class balance is preserved in both sets.
from sklearn.model_selection import train_test_split
X = df.drop(columns=["target"])
y = df["target"]
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.2,
random_state=42, # reproducible split
stratify=y, # keep class balance (classification)
)
print(X_train.shape, X_test.shape)Why: every transformer LEARNS parameters (a mean, a category list, a min/max) and must learn them from training data only, then apply them unchanged to the test set. When: call fit on train, transform on both — never fit on test. Where: this fit/transform split is the discipline the whole course builds on, and pipelines enforce it for you.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train[["age"]]) # LEARN from train only
X_train["age"] = scaler.transform(X_train[["age"]]) # apply
X_test["age"] = scaler.transform(X_test[["age"]]) # apply the SAME params
# fit_transform on train is shorthand; on test you ONLY transform.