A Pipeline chains preprocessing steps and a final model into a single estimator. It's not just tidy — it's the correct way to prevent data leakage, because every step is fit on training data only, automatically.
Building a pipeline
from sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerfrom sklearn.linear_model import LogisticRegressionpipe = make_pipeline(StandardScaler(),LogisticRegression(),)pipe.fit(X_train, y_train) # scaler.fit_transform then model.fit — on trainpipe.predict(X_test) # scaler.transform then model.predict — on test
Why pipelines matter
Key takeaways
- 01A Pipeline chains preprocessing steps and a model into one estimator.
- 02Every step is fit on training data only — leakage-safe by construction.
- 03It behaves like any estimator: fit/predict/score.
- 04Pipelines make cross-validation and grid search correct (preprocessing re-fit per fold).
