devnotes.

Pipelines for Clean ML Workflows

A Pipeline chains preprocessing steps and a model together into a single object — preventing a specific, easy-to-make mistake and making hyperparameter tuning across the whole workflow straightforward.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV

pipeline = Pipeline([
    ("scaler", StandardScaler()),
    ("classifier", RandomForestClassifier(random_state=42)),
])

pipeline.fit(X_train, y_train)          # scales, THEN fits the model, in one call
predictions = pipeline.predict(X_test)   # scales test data with the SAME fitted scaler, then predicts

# Tuning hyperparameters across both the preprocessing and the model, together:
param_grid = {
    "classifier__n_estimators": [50, 100],    # note the double underscore: step_name__param_name
    "classifier__max_depth": [5, 10, None],
}
grid_search = GridSearchCV(pipeline, param_grid, cv=5)
grid_search.fit(X_train, y_train)

A Pipeline bundles multiple steps — typically preprocessing (scaling, encoding) followed by a final model — into one object with the same .fit()/.predict() interface as any single estimator. Calling .fit() on the pipeline fits the scaler on the training data and trains the model in one call; calling .predict() automatically applies the already-fitted scaler's transformation before passing data to the model — exactly enforcing the training-data-only-fitting rule covered in the preprocessing chapter, this time by construction rather than by remembering to do it manually.

Beyond preventing that specific data-leakage mistake, pipelines make code meaningfully cleaner: one .fit() call instead of several manual steps, and one object to pass around, save, or deploy instead of juggling a scaler and a model separately. The step_name__param_name syntax (double underscore) in the tuning example lets GridSearchCV reach into and tune hyperparameters belonging to any step of the pipeline, not just the final model — pipelines and hyperparameter search compose together naturally, and building this habit from early on is one of the most reliable ways to avoid subtle preprocessing bugs in real projects.

Pipelines for Clean Machine Learning Workflows | DevNotes