Machine Learning
Advantages
Pipeline prevents data leakage and keeps code clean.
What a pipeline gives you
A pipeline gives three concrete gains:
- Correctness: leakage is prevented structurally.
- Convenience: one
fit, one predict; in grid search the settings of all steps are searched together. - Portability: the whole chain is saved as one object and moved to production.
import joblib
joblib.dump(pipe, "model.joblib")
yuklu = joblib.load("model.joblib")
yuklu.predict(yeni_veri) # raw data is enough
Raw data arrives in production; if preprocessing isn't saved with the model, the same transformation as in training must be rewritten, which creates a risk of errors.
A grid inside a pipeline
In a grid search, both preprocessing and model settings are searched together; each combination is retrained cleanly in every fold:
izgara = {
"olcek": [StandardScaler(), MinMaxScaler()],
"model__C": [0.1, 1, 10],
}
gs = GridSearchCV(pipe, izgara, cv=5).fit(X_train, y_train)
print(gs.best_params_)
Both the scaler choice and the C value are decided by validation.
Exercise Save and load
Save the trained pipeline to the file "hat.joblib" with joblib.dump and load it back with joblib.load. Print on separate lines that the loaded model's test predictions match the original's, using (... == ...).all(), and the loaded model's test accuracy with round(..., 3).
Show example solution
import joblib
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipe = make_pipeline(StandardScaler(), LogisticRegression()).fit(X_train, y_train)
joblib.dump(pipe, "hat.joblib")
yuklu = joblib.load("hat.joblib")
print((yuklu.predict(X_test) == pipe.predict(X_test)).all())
print(round(yuklu.score(X_test, y_test), 3))
Test yourself
-
What does forgetting to save the scaler separately when moving a model to production lead to?
- The model runs faster
- Nothing
- The model retrains itself
- The model makes wrong predictions on unscaled data
Show answer
The model makes wrong predictions on unscaled data — The model was trained on scaled data; raw data is in a different space.
-
Save the pipeline to a file.
joblib.____(pipe, "model.joblib")
Show answer
joblib.dump(pipe, "model.joblib")
-
Match the gains.
- Leak prevention
- Correctness
- A single fit
- Convenience
- joblib.dump
- Portability
- "model__C"
- Joint search
Next lesson: ColumnTransformer