Giter Site home page Giter Site logo

Comments (6)

ageron avatar ageron commented on May 17, 2024 1

Hi @Ayeness ,
Thanks for your kind words! :)
Yes, you are correct: you do not have to build a pipeline, you can just apply the StandardScaler manually like this:

X_train_scaled = scaler.fit_transform(X_train)
X_valid_scaled = scaler.transform(X_valid)
X_test_scaled = scaler.transform(X_test)

lin_reg = LinearRegression()
lin_reg.fit(X_train_scaled, y_train)
score = lin_reg.score(X_valid_scaled, y_valid)

That said, you could also create a Pipeline containing the StandardScaler and the LinearRegression: it would simplify the code slightly:

pipeline = Pipeline([
  ("scaler", StandardScaler()),
  ("lin_reg", LinearRegression())
])
pipeline.fit(X_train, y_train)
score = pipeline.score(X_valid, y_valid)

Hope this helps!

from handson-ml2.

ageron avatar ageron commented on May 17, 2024 1

Hi @Ayeness ,

Great work! You're almost there. The pipeline takes care of transforming the inputs, so you don't need to do it explicitly. You can replace these two lines:

some_data_prepared = pipeline.transform(some_data)
print('Prediction:', lin_reg.predict(some_data_prepared))

With this line:

print('Prediction:', lin_reg.predict(some_data))

Hope this helps!

from handson-ml2.

ssilverac avatar ssilverac commented on May 17, 2024 1

Hi @Ayeness ,
Great work! You're almost there. The pipeline takes care of transforming the inputs, so you don't need to do it explicitly. You can replace these two lines:
some_data_prepared = pipeline.transform(some_data)
print('Prediction:', lin_reg.predict(some_data_prepared))
With this line:
print('Prediction:', lin_reg.predict(some_data))
Hope this helps!

thank you for the information @ageron
i just wanted to make this comment in case anyone in the future stumbles upon this thread, you have a small typo.

we should be replacing those first two lines with

print('Prediction:', pipeline.predict(some_data))

not
lin_reg.predict(some_data)

from handson-ml2.

ssilverac avatar ssilverac commented on May 17, 2024

@ageron thanks for the advice.
I have tried to implement your suggestion with the following code

from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression

pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('lin_reg', LinearRegression())
])


pipeline.fit(boston_df, boston_df_target)

but then when I try to test this on some instances from the training set:

some_data = boston_df.iloc[:5]
some_targets = boston_df_target.iloc[:5]

some_data_prepared = pipeline.transform(some_data)
print('Prediction:', lin_reg.predict(some_data_prepared))
print('Labels:', list(some_targets))

I get the following error message

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-131-d8c9add2e0df> in <module>
      2 some_targets = boston_df_target.iloc[:5]
      3 
----> 4 some_data_prepared = pipeline.transform(some_data)
      5 print('Prediction:', lin_reg.predict(some_data_prepared))
      6 print('Labels:', list(some_targets))

~\Anaconda3\envs\ml_book\lib\site-packages\sklearn\pipeline.py in transform(self)
    532         # XXX: Handling the None case means we can't use if_delegate_has_method
    533         if self._final_estimator != 'passthrough':
--> 534             self._final_estimator.transform
    535         return self._transform
    536 

AttributeError: 'LinearRegression' object has no attribute 'transform'

do you know what a possible reason for this error could could be?

from handson-ml2.

ageron avatar ageron commented on May 17, 2024

To be clear, under the hood:

  • pipeline.fit(boston_df, boston_df_target) will do lin_reg.fit(scaler.fit_transform(boston_df), boston_df_target).
  • and pipeline.predict(some_data) will do lin_reg.predict(scaler.transform(some_data)).

from handson-ml2.

ssilverac avatar ssilverac commented on May 17, 2024

Thank you for the insight. I will try this out

from handson-ml2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.