Packaging & deployment
P13.ml-lifecycle.02 · Audience: guest, it-ml, language-pro · Prerequisites: Evaluation infrastructure & experiment tracking
A fitted model living in a notebook's memory is a fact about your machine, not a shippable artefact. Deployment is the discipline of turning that in-memory object into a byte-exact, verifiable, documented package a stranger (or a serving container, or you in six months) can reload and get the same predictions from. P13.ml-lifecycle.01 made evaluation reproducible; this module makes the model itself reproducible — serialisation, a checksummed manifest, a model card, and the round-trip contract that ties them together.
When you call pipeline.fit(X, y) the result is a graph of Python objects on a heap — coefficients, fitted encoders, learned vocabularies — reachable only from a variable in one interpreter. Close the notebook and it is gone. To ship a model you must turn that live graph into bytes on disk that another process can read back into an equivalent object. That is serialisation.
ⓘ Concept: The load-and-score contract
load(path) → model, then model.predict(rows) → predictions. The serving layer knows nothing of how the model was trained; it only depends on that two-call contract. Packaging is the work of making that contract hold across machines and time.Two serialisers dominate the Python ML world:
- pickle (stdlib) — serialises almost any Python object by reference to its class. Universal, but the class must be importable at load time and the format is not a security boundary (never unpickle untrusted bytes).
- joblib — pickle-compatible but optimised for large NumPy arrays (memory-mapped, compressed). The de-facto default for scikit-learn estimators; the P5 case-01 pipeline ships as
pipeline.joblib.
The failure mode this prevents is "works on my machine": the bytes reload fine on the author's laptop but explode in the serving container because a class moved, a dependency version drifted, or the file was silently truncated in transit. The rest of this module removes each of those ambiguities in turn.
Ask the mentor about this module
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
🎓 Practice ladder
3 graded rungs · ~30 minBuild the packaging core by hand: a sha256 manifest hasher, a byte-integrity verifier that reports corrupt or missing files on reload, and the round-trip contract — pickle a model, reload it in a fresh object, and prove the predictions are identical. All stdlib; no network, no sklearn required.
Rung 1 — a sha256 manifest
Loading exercise…
Rung 2 — verify byte-integrity on reload
Loading exercise…
Rung 3 — the round-trip reproducibility contract (senior)
Loading exercise…
⚡ Interview Ref — the quick-scan Reference face
- A fitted model is not shippable — it's an object graph on one heap. Deployment = serialise it to bytes behind a fixed
load(path) → model/model.predict(rows)contract. - Serialisers:
pickle(stdlib, class must be importable at load, not a security boundary) ·joblib(pickle-compatible, optimised for large NumPy arrays, sklearn default). - Pin three things: code (git commit / model class) · data (dataset id + version + hash) · deps (lockfile). Bytes alone don't reproduce.
- Manifest = per-file
{filename, bytes, sha256}. Consumer re-hashes on reload and rejects a corrupt/missing file before deserialise. sha256, not length — length misses corruption. - Model card documents: data + licence · split protocol + seed · held-out metrics · intended use · caveats / non-uses. Ships inside the package (manifest-listed, checksummed).
- Reproducibility contract = serialise → reload in a fresh namespace → identical predictions. That round-trip is the acceptance test of packaging; fresh namespace catches baked-in globals & leaked seeds.
- "Works on my machine" is the failure mode packaging removes: moved class, drifted dep, truncated file — each pinned or checksummed away.
📚 Go Further
Where the pieces of this module lead.
| Type | Resource |
|---|---|
| In-app | P13.ml-lifecycle.01 — Evaluation infrastructure & experiment tracking: the gate that decides a model may ship, before this module packages it |
| In-app | P5.applied-cases.01 — The honest tabular model: the fitted Pipeline + model card this module packages (ships as pipeline.joblib) |
| In-app | P13.cases.01 — the end-to-end serving-and-drift case that threads a checksummed, card-carrying package into production |
| Docs | joblib persistence & scikit-learn model persistence guide; Python hashlib / pickle stdlib docs |
| Paper | Mitchell et al. (2019), Model Cards for Model Reporting — the documentation template this module packages |
Try it yourself
A scratch console for this page's ideas — ungraded, nothing you run here is recorded.
Scratch console
A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.
Output appears here.
Where next?
This module unlocks
Go up a level