CodeBlanks Logo Icon CodeBlanks

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.

Practice 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).

Starter code

model.py
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])

# buraya yaz

Sample solution

model.py
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))

Expected output

[0]
[[ 0.    -0.708]]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which method does <code>StandardScaler</code> run during <code>pipe.predict(X_test)</code>?

  • transform Correct answer
  • fit_transform
  • fit
  • predict

Explanation: No step relearns in the prediction phase.

2 Apply the transformations before the model.

Question
Z = pipe____.transform(X_test)
Answer
Z = pipe[:-1].transform(X_test)

Explanation: pipe[:-1] contains all transformers except the last step.

3 Match the flows.

Term Meaning
pipe.fit fit_transform on transformers
pipe.predict transform on transformers
pipe.predict_proba Last step's probability
pipe[:-1] Intermediate data

All lessons in Intermediate