Data analysis and machine learning with Python · Lesson 3 of 3
Machine-learning pipelines and reproducibility
Package preprocessing and estimation into a reproducible model workflow.
Lesson
1. Concept and mental model
A machine-learning pipeline records the ordered transformations needed to turn raw features into a prediction. Fit learned preprocessing only on training data, then reuse the fitted steps for validation, test, and production inputs. Pipelines reduce leakage and make cross-validation more reliable because each fold learns its own preprocessing state. Keep feature names, expected types, and category behavior explicit at the model boundary.
2. How it works
Reproducibility requires more than saving a model file. Record the training data version, feature definitions, library versions, random seeds where relevant, evaluation metrics, and code revision. Data drift or a changed upstream schema can invalidate assumptions even if the serialized model loads successfully. Validate inputs at prediction time and provide a safe response when required features are absent or malformed.
3. Use it safely and effectively
Model persistence formats can execute code or carry security risk depending on the serialization tool. Load only trusted artifacts from controlled storage, and restrict who can publish them. Before deployment, compare predictions from the packaged artifact to the validated development pipeline on a fixed test set. Practice by saving a pipeline in a controlled development environment and checking that predictions remain consistent after reload.
4. Syntax reference
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
pipeline = make_pipeline(SimpleImputer(strategy='median'), RandomForestClassifier(random_state=7))
pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)5. Guided walkthrough
Split data before fitting any transformation, put preprocessing and estimation into one pipeline, and record the data and software versions used. Compare repeated runs using fixed split seeds while remembering that a single split may not represent every future population.
6. Common mistakes to avoid
- Preprocessing the full dataset before splitting and allowing test information into training.
- Saving a model without the preprocessing, feature order, or metadata required to use it correctly.
Visual guide
Reproducible prediction pipeline
-
Step 01
Validate
Check the incoming feature schema.
-
Step 02
Transform
Apply fitted preprocessing steps.
-
Step 03
Predict
Run the versioned estimator.
Code examples
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
pipeline = make_pipeline(SimpleImputer(strategy='median'), RandomForestClassifier(random_state=7))
pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)
A reproducible pipeline applies imputation before producing predictions.
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipeline = make_pipeline(StandardScaler(), LogisticRegression())
pipeline.fit(X_train, y_train)
print(pipeline.score(X_test, y_test))
The held-out score for a pipeline fitted only on training data.
import joblib
joblib.dump(pipeline, 'model.joblib')
restored = joblib.load('model.joblib')
print(restored.predict(X_test[:1]))
A prediction produced by the restored pipeline.
Key points
- Package preprocessing and estimation into a reproducible model workflow.
- Prefer explicit, testable behavior over hidden side effects.
- Validate assumptions at system boundaries and handle failure deliberately.
Check your understanding
Practice
Your learning progress