Build a Reproducible Machine Learning Pipeline

MetaCyberGuru Academy

IntermediateEstimated learning effort: 90 minutesFree, no sign-up requiredPublished by Muhammad AzharCourse version: August 2026

Back to Evaluation and Reliable Machine Learning Pipelines

A reliable pipeline does the same transformations during training, evaluation and later prediction. It also preserves feature meaning, fitted parameters and the assumptions needed to interpret the result.

Package the full procedure, not only the estimator

You will process numerical and categorical fields with a ColumnTransformer, fit one classifier and serialize a compact evaluation record.

  • Apply different preprocessing to numerical and categorical columns.
  • Keep imputation and encoding inside the fitted pipeline.
  • Use deterministic splitting and record software, data and metric details.
  • Write a small model card that names intended use and limitations.

The procedure is the model

A fitted estimator depends on the exact columns, category handling, imputation and scaling that produced its matrix. Saving only the last object invites training-serving skew. A pipeline keeps the ordered steps together and validates input shape at prediction time.

ColumnTransformer routes named fields to separate preprocessing branches. Numerical features might use median imputation and scaling. Categorical features might use most-frequent imputation and one-hot encoding with unknown categories ignored. Ignoring an unknown category prevents a crash, but it also means the new value contributes no known category signal. Monitor it.

Reproducibility includes data identity, split logic, target definition, random seed, dependency versions and code version. A model file without this record cannot explain how it was produced. Do not store credentials or raw personal records inside metadata.

A model card states intended users and uses, evaluation conditions, limitations, ethical considerations and maintenance contacts. It is a living operational document, not a marketing certificate. Link claims to actual evaluation artefacts.

Serialization can execute code when loaded. Treat model artefacts as trusted binaries, restrict write access and prefer rebuilding from source when provenance is uncertain.

Fit mixed data in one pipeline

This compact example predicts a synthetic renewal label. It verifies that a previously unseen region does not crash prediction.

Install the lesson dependencies

python -m pip install pandas scikit-learn

Route columns and keep transformations attached

import json
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

train = pd.DataFrame({
    "tenure": [2, 8, 14, 20, 3, 18],
    "monthly_orders": [1, 3, 5, 6, 1, 5],
    "region": ["N", "S", "N", "W", None, "S"],
    "renewed": [0, 0, 1, 1, 0, 1],
})

numeric = Pipeline([("impute", SimpleImputer(strategy="median")), ("scale", StandardScaler())])
categorical = Pipeline([("impute", SimpleImputer(strategy="most_frequent")),
                        ("encode", OneHotEncoder(handle_unknown="ignore"))])
prepare = ColumnTransformer([("num", numeric, ["tenure", "monthly_orders"]),
                             ("cat", categorical, ["region"])])
pipeline = Pipeline([("prepare", prepare), ("model", LogisticRegression(max_iter=1000))])

X = train.drop(columns="renewed")
y = train["renewed"]
pipeline.fit(X, y)
prediction = pipeline.predict(X)
future = pd.DataFrame({"tenure": [10], "monthly_orders": [4], "region": ["E"]})

card = {
    "target": "renewed",
    "unit": "synthetic customer record",
    "metric": {"training_accuracy": float(accuracy_score(y, prediction))},
    "limitation": "training score on six synthetic rows is not a generalization estimate",
}
print("future prediction:", pipeline.predict(future).tolist())
print(json.dumps(card, indent=2))

Expected structure

future prediction: [<0 or 1>]
{
  "target": "renewed",
  "unit": "synthetic customer record",
  "metric": {
    "training_accuracy": <value>
  },
  "limitation": "training score on six synthetic rows is not a generalization estimate"
}

The example reports training accuracy only to demonstrate metadata. It explicitly refuses to present that score as evidence of generalization.

Pipeline failures to test deliberately

Test bad inputs before deploying. Helpful schema errors are safer than a plausible score from the wrong columns.

  • A numeric column arriving as currency text should fail or use an explicit parser, not silently become missing.
  • Unexpected categories should be counted and monitored even when the encoder ignores them.
  • A reordered DataFrame should work by name, while a raw array can silently swap meanings.
  • Loading an untrusted pickle or joblib file can execute malicious code.

Extend the procedure responsibly

Add a true held-out evaluation, a schema validator and a versioned model card to your current project.

  • Use cross-validation for selection and an untouched final test split.
  • Record input schema, target definition and feature cutoff.
  • Add tests for a missing column, extra column and unseen category.
  • Save a requirements or lock file and a rebuild command.

Pipeline evidence

  • Runnable training and prediction commands.
  • Model card linked to evaluation output.
  • Schema and unknown-category tests.

Knowledge check

1. Why put preprocessing inside the pipeline?
Check your reasoning

The pipeline binds transformation fitting to training partitions and reuses it consistently at prediction.

2. What does handle_unknown=’ignore’ do?
Check your reasoning

The input can pass, but the unfamiliar category contributes no learned category indicator and should be monitored.

3. Why is training accuracy on six rows not generalization evidence?
Check your reasoning

Evaluation on training data measures fit to seen cases, not performance on unseen data.

Official references and further reading

Review note for Build a Reproducible Machine Learning Pipeline: recheck the linked documentation after a dependency changes the relevant API, metric or modelling assumption, then record the tested version beside your result.

Save your place

Completion is stored only in this browser on this device.

Share this page

Share this page with the people who will use it next.

X Facebook LinkedIn WhatsApp Email

Discussion

No comments yet. Add the first useful question or observation.