Machine Learning Basics·Machine learning from data to a responsible model

Pipelines

Chain preprocessing and models so evaluation cannot leak.

Intermediate 14 min read Lesson 9 of 300 Challenge included
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.

pipeline.pyPython
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

Build a pipeline

Easy

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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 9 of 300 · 0% of course