ML2: Preparing data without fooling yourself

Lesson 4 of 5 · reading 15–25 minutes; practice and project time is on the module page

Pipelines: the same steps in training and in use

🎯You'll be able to

Put preparation and the model into one scikit-learn Pipeline, fit it on training data only, and save it as one object.

🏠The everyday picture

A restaurant that writes its recipe down, with exact amounts, gets the same dish whoever cooks. If each cook measures "a pinch" their own way, the dish changes. A pipeline is the written recipe: every step, in order, saved together.

📘What it really is

📝A worked example

Input:

from sklearn.pipeline import Pipeline
from sklearn.ensemble import HistGradientBoostingClassifier
from dakdost.train import encoder, split_by_time, load
from dakdost.features import build_frame

train, valid, test = split_by_time(load())
model = Pipeline([('prep', encoder(False)),
                  ('clf', HistGradientBoostingClassifier(max_iter=200, learning_rate=0.05, max_leaf_nodes=15,
                                                         l2_regularization=1.0, random_state=0))])
model.fit(build_frame(train), train['late'])
print(model.predict_proba(build_frame(valid))[:3, 1].round(4))

Expected output: three probabilities between 0 and 1 for the first three May parcels. Everything the model learned, including the encoder's categories, came from January to April only.

🌍Where it's used

📝Case scenario

A lending app's model worked in testing but rejected many good applicants in the app. The data team had scaled income in rupees per month; the app sent income per year. The model saw incomes twelve times larger than any it had learned from. Saving the scaler in the pipeline, and validating the input's unit, fixed it.

🖱️Try it

Train the pipeline above, save it with joblib.dump(model, 'models/try.joblib'), load it in a new Python session and predict the same three May parcels. Success: identical probabilities.

⚠️Common mistakes

🛠️For your project

Project 2's test 5 checks that the API's probability equals the saved pipeline's for the same parcel at four booking times.

✅Quick check

Why save the whole pipeline instead of just the model?

Show the answer

So that new data gets exactly the same preparation as the training data.

What is training/serving skew?

Show the answer

The model receiving inputs prepared differently in use from how they were prepared in training.

← PreviousTurning categories and numbers into model inputs
ProLearners Assist