Machine Learning
fit and predict
In a Pipeline, fit() trains all steps, predict() applies all steps.
The flow of fit and predict
In a pipe.fit(X, y) call, fit_transform is called for each transformer and the result passes to the next step; only fit is called on the last step. pipe.predict(X) calls only transform on each transformer:
# fit: X -> doldur.fit_transform -> olcek.fit_transform -> model.fit
# predict: X -> doldur.transform -> olcek.transform -> model.predict
A pipeline offers every method its last step offers: predict_proba, decision_function, score.
Seeing the intermediate result
Looking at the transformed data through a sub-pipeline makes debugging easier. For example, to verify that scaling really zeroed the mean:
Z = pipe[:-1].transform(X_train)
print(Z.mean(axis=0).round(3)[:3]) # [0. 0. 0.]
print(Z.shape)
pipe[:-1] isn't a new object but a view sharing the same trained steps.
Exercise Fill, scale, predict
Build and train a make_pipeline with SimpleImputer(strategy="mean"), StandardScaler() and LogisticRegression() for X, which has missing values. Print the prediction for [[np.nan, 3.0]], then the result of pipe[:-1].transform([[np.nan, 3.0]]) with np.round(..., 3).
Show example solution
import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
X = np.array([[1.0, 1.0], [2.0, np.nan], [3.0, 2.0], [7.0, 6.0], [8.0, 7.0], [np.nan, 8.0]])
y = np.array([0, 0, 0, 1, 1, 1])
pipe = make_pipeline(SimpleImputer(strategy="mean"), StandardScaler(), LogisticRegression()).fit(X, y)
print(pipe.predict([[np.nan, 3.0]]))
print(np.round(pipe[:-1].transform([[np.nan, 3.0]]), 3))
Test yourself
-
Which method does <code>StandardScaler</code> run during <code>pipe.predict(X_test)</code>?
- transform
- fit_transform
- fit
- predict
Show answer
transform — No step relearns in the prediction phase.
-
Apply the transformations before the model.
Z = pipe____.transform(X_test)
Show answer
Z = pipe[:-1].transform(X_test)
-
Match the flows.
- pipe.fit
- fit_transform on transformers
- pipe.predict
- transform on transformers
- pipe.predict_proba
- Last step's probability
- pipe[:-1]
- Intermediate data
Next lesson: Advantages