Pipelines
Chain preprocessing and models so evaluation cannot leak.
What you will be able to do
- Put every learned transformation inside the pipeline.
- Use ColumnTransformer for mixed types.
- Cross-validate the whole pipeline.
- Save the pipeline, not just the model.
In plain English
A Pipeline runs preprocessing and the model as one object. It fits every step on training data only, which prevents leakage. Use ColumnTransformer to apply different steps to numeric and categorical columns.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipe = Pipeline([
('scale', StandardScaler()),
('model', LogisticRegression(max_iter=1000)),
])
pipe.fit(X_train, y_train)
print(pipe.predict(X_test)[:5])Your turn
EasyBuild a pipeline
Chain StandardScaler and LogisticRegression in a Pipeline.
PYTHON · starter
pipe = Pipeline([])Check your understanding before moving on.
Knowledge checkQuestion 1
What does a Pipeline guarantee during cross-validation?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.