AI Academy

Structured tracks from the basics to generative AI and agents, with sequential lessons and quizzes.

201lessons

MLOps: Deploying and Operating ML Models Intermediate 12 minutes

ML Pipelines

From scattered scripts to a single repeatable pipeline that prevents leakage and simplifies deployment.

Your progress in the track0%

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 saved

System-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

ToolSuits
DVC pipelinesResearch projects and small teams
Prefect / AirflowScheduling recurring data jobs
Kubeflow / Vertex / SageMaker PipelinesLarge 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

  1. How does a Pipeline prevent leakage in cross-validation?

    Because the preparation steps are trained inside each fold on the training part only.

  2. 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

  1. scikit-learn — Pipelines and composite estimators
  2. DVC — Data Pipelines
  3. Apache Airflow Documentation

Complete the lesson