Comments (6)
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.
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.
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.
@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.
To be clear, under the hood:
pipeline.fit(boston_df, boston_df_target)
will dolin_reg.fit(scaler.fit_transform(boston_df), boston_df_target)
.- and
pipeline.predict(some_data)
will dolin_reg.predict(scaler.transform(some_data))
.
from handson-ml2.
Thank you for the insight. I will try this out
from handson-ml2.
Related Issues (20)
- CH12: Custom Models: number of iterations for lower Residual Block HOT 1
- [BUG] Chapter 10: KerasRegressor vs Sci-Keras issue
- Where Can I find Appendix A? Is it in book or files? If files where? HOT 1
- [QUESTION] Chapter 14 error
- [BUG] [Chapter2] ValueError when calculating the correlations in corr_matrix = housing.corr() HOT 3
- [BUG] Chapter 4, Notebook: 04_training_linear_models.ipynb, Cell 47 HOT 4
- I get "TypeError: 'module' object is not subscriptable" for cm module HOT 2
- [BUG] 'my_mnist.data' HOT 1
- Chapter 10 neither attribute keras.wrapper nor keras.__version__ being found :/ HOT 1
- [QUESTION] Chapter 10 Exercise 10 Factor parameter HOT 1
- [QUESTION]What is the difference between predict_proba, decision_function and predict function and how to choose HOT 1
- [BUG] HOT 1
- [BUG]sgd_reg = SGDRegressor(max_iter=1, tol=-np.infty, warm_start=True, penalty=None, learning_rate="constant", eta0=0.0005, random_state=42)
- [BUG]X = iris["data"][:, 3:] # petal width y = (iris["target"] == 2).astype(np.int) # 1 if Iris virginica, else 0
- [BUG]09 unsupervised learning
- [QUESTION] Chapter 10 RandomizedSearchCV returned best model having worse performance. HOT 1
- [QUESTION] Chapter 2 StratfiedShuffleSplit
- Machine Learning
- [BUG] [Chapter 4]: Equation rendering issue in "exercise 12" solution in 04_training_linear_models.ipynb
- [QUESTION] About Cross-Validation in Chapter2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from handson-ml2.