Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 2 (page90), full_pipeline.transform() #637

Open
luluqinqin opened this issue Sep 3, 2021 · 1 comment
Open

Chapter 2 (page90), full_pipeline.transform() #637

luluqinqin opened this issue Sep 3, 2021 · 1 comment

Comments

@luluqinqin
Copy link

luluqinqin commented Sep 3, 2021

Hi Aurélien,

Thank you for a GREAT book! I really enjoy reading it and practicing the examples in the book.
In chapter 2, when we need to transform the test data, you mention that not using the fit_transform() as we don't want to fit the test data, and we should use the transform() function. But the SimpleImputer needs to run the fit() first before running the transform(). Can you please illustrate a solution for that? Thank you!!

Lu

Code:

num_pipeline = Pipeline([
    ('imputer', SimpleImputer(strategy = 'median')),
    ('std_scaler', StandardScaler()),
])

#
num_attr = list(test_num)
cat_attr = ['ocean_proximity']   
full_pipeline = ColumnTransformer([
        ("num", num_pipeline, num_attr),
        ("cat", OneHotEncoder(), cat_attr),
    ])
test_x_tr = full_pipeline.transform(test_x) 
test_x_tr

Error:

NotFittedError: This ColumnTransformer instance is not fitted yet. Call 'fit' with
appropriate arguments before using this estimator.
@ageron
Copy link
Owner

ageron commented Oct 6, 2021

Hi @luluqinqin ,

Thanks for your kind words! 😊 And sorry for the late reply...

You are right that you must always call fit() on a transformer before you can call transform() (or of course you can call fit_transform(), which does both). Similarly, you must always call fit() on a predictor before you call predict().

BUT, the call to fit() must always be done on the training set. Once that's done, you can call transform() (or predict(), in the case of a predictor) on the training set or any other set.

So, your code would be correct if we replaced the last two lines with this code:

train_x_tr = full_pipeline.fit_transform(train_x) 
test_x_tr = full_pipeline.transform(test_x)

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants