ML (Beginner) · Evaluate & Ship · cozy lesson
Your First End-to-End Model 🔒 Premium
🔒 Premium chapter — free for early learners.
14 min · 2 min read · no scary math, promise
The whole track in one script
Everything you've learned — cleaning, splitting, training, evaluating — assembles into a single Pipeline object that carries data from raw CSV to prediction with no leakage gaps. This is what "shipping a model" actually looks like at beginner scale: 40 lines, no magic.
Read it top to bottom
import joblib
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestClassifier
from sklearn.impute import SimpleImputer
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
pipe = Pipeline([
("prep", ColumnTransformer([
("num", SimpleImputer(strategy="median"), ["age", "bill"]),
("cat", OneHotEncoder(handle_unknown="ignore"), ["city"]),
])),
("model", RandomForestClassifier(n_estimators=200, random_state=7)),
])
print("CV:", cross_val_score(pipe, X, y, cv=5).mean())
pipe.fit(X, y)
joblib.dump(pipe, "churn_model.pkl")
Three ideas doing the heavy lifting: the ColumnTransformer applies different cleaning per column type; cross_val_score rotates validation slices so the number is trustworthy; the Pipeline wraps it all so preprocessing and model travel together.
Why pipelines prevent the classic disaster
Without one, it's easy to fit the imputer on full data, tune on test, or serve with different preprocessing than training — three flavors of leakage and skew. Inside a pipeline, the same steps run in training, validation, and serving. What you evaluated is literally what you ship: joblib.load("churn_model.pkl").predict(new_row).
After shipping: watch it like software
Models rot — customer behavior shifts, features drift. Monitor prediction distributions and business metrics like any other dashboard, keep a data snapshot + code version per model file, and schedule retraining before decay becomes an incident.
Remember this
- Pipeline = cleaning + model in one leak-proof object. Cross-validate, dump with joblib, version everything.
- You now own the full loop: load → clean → train → evaluate → save → monitor.
Check your understanding
Correct answers earn XP (once each).
1. Pipeline object buys you…
2. Ship it how?
My notes (saved in this browser)
Select text above → Save selection, or write your own. Your notebook lives in this browser.
No notes yet. Your highlights will live here.
References
Finished reading? Seal it with a tick ✅
The checkbox in the explorer turns green too — same progress.