ML Pipelines
From scattered scripts to a single repeatable pipeline that prevents leakage and simplifies deployment.
A pipeline in scikit-learn
import joblib
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
num, cat = ["age", "glucose"], ["sex", "region"]
pre = ColumnTransformer([
("num", Pipeline([("imp", SimpleImputer(strategy="median")), ("sc", StandardScaler())]), num),
("cat", OneHotEncoder(handle_unknown="ignore"), cat),
])
pipe = Pipeline([("pre", pre), ("clf", LogisticRegression(max_iter=1000))])
pipe.fit(X_train, y_train)
joblib.dump(pipe, "model.joblib") # the whole pipeline is savedSystem-level pipelines
A simplified dvc.yaml file:
stages:
prepare:
cmd: python src/prepare.py
deps: [src/prepare.py, data/raw]
outs: [data/processed]
train:
cmd: python src/train.py
deps: [src/train.py, data/processed]
params: [train.lr, train.epochs]
outs: [models/model.joblib]
metrics: [metrics.json]The command dvc repro reruns only the stages whose inputs have changed.
Orchestration tools
| Tool | Suits |
|---|---|
| DVC pipelines | Research projects and small teams |
| Prefect / Airflow | Scheduling recurring data jobs |
| Kubeflow / Vertex / SageMaker Pipelines | Large cloud platforms |
A general principle: each step is as pure a function as possible, its inputs and outputs are specific files or tables, and its settings are in a single file.
Lesson terms
- Directed acyclic graph (DAG)
- A structure that describes tasks and the dependencies between them without cycles, used to organize the stages of a workflow.
- The pipeline
- A defined sequence of processing steps and a model that is trained and applied as a single unit.
Abstract
A processing pipeline links the preparation and training steps in a single object that is trained, saved and applied as one unit, which prevents statistics leaking from the test set and ensures that the same processing is applied at inference time. At the system level, tools such as DVC pipelines, Airflow, Prefect or Kubeflow organize the stages of data extraction, validation, training, evaluation and registration, and rerun only what has changed.
Review questions
-
How does a Pipeline prevent leakage in cross-validation?
Because the preparation steps are trained inside each fold on the training part only.
-
What is the benefit of the directed graph in orchestration tools?
It defines the order of the stages and their dependencies, so only the affected stages are rerun when their inputs change.
Sources and references
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate