Package preprocessing with the trained model
In this article (5 sections)
A classifier depends on the transformations that created its feature matrix. Deploying weights while rewriting scaling and category encoding in service code creates training-serving skew. Package the fitted preprocessing and estimator together, then test reloaded predictions.
Reload the full pipeline
The deployment lab combines a ColumnTransformer with numeric scaling, one-hot encoding and logistic regression. It saves a trusted temporary artifact and reloads it.
from deployment_cases import package_case
result = package_case()
assert result["steps"] == ["preprocess", "classifier"]
assert result["maximum_prediction_difference"] == 0.0
print(result["artifact_bytes"])Forty held-out probabilities match exactly before and after reload. The artifact is 3,297 bytes in the recorded run. Size can vary with library serialization; prediction equivalence is the key check.
Preserve feature contracts
Store input column names, units, dtypes, missing-value behavior and categorical unknown handling. The lab’s encoder ignores an unknown plan by producing no active known-plan column; production teams must decide whether that fallback is acceptable or should trigger review.
Version preprocessing with the model and include a golden request batch. Test the actual service path after load, not only the estimator object. Record Python and package versions because scikit-learn persistence generally expects a compatible environment.
Load only trusted artifacts
Joblib and pickle-style formats can execute code during deserialization. A successful local round trip does not make them safe for user uploads or arbitrary downloaded files. Restrict provenance and access, verify integrity, and consider safer formats that meet the estimator’s needs.
The Data Science course joins model packaging with schema, integrity and environment evidence.
Exercise
Change a category order in a separately coded preprocessor and show prediction drift. Restore the packaged pipeline, add a golden batch and verify equality in a fresh process.
Continue learning
This article is part of the Model deployment and MLOps sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Test batch and single-record prediction consistency.
- Continue with Design a model API error contract.
Reference: scikit-learn model persistence.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.
Explore Data Science