[scikit-learn] using a predictor as transformer

Andreas Mueller t3kcit at gmail.com
Wed Sep 6 13:49:31 EDT 2017


If you want to use lasso for feature selection in a pipeline you have to 
wrap it in SelectFromModel.


On 09/06/2017 11:56 AM, Lefevre, Augustin wrote:
>
> Hi all,
>
> I am playing with the pipeline features of sklearn and it seems that I 
> can’t use a prediction algorithm as intermediate step.
>
> For instance, in the example below I use the output of a lasso as an 
> additional feature to feed a random forest, in such a way that feature 
> selection the Lasso does some preliminary feature selection.
>
> But I get a : “TypeError: All estimators should implement fit and 
> transform.”
>
> So I would like to add a transform method to the Lasso estimator so 
> that it can be used in a FeatureUnion. Is that possible ?
>
> Best regards
>
> X=np.hstack((np.random.randn(500,10),np.random.randint(0,10,(500,10)))) 
> /# regressor variables
> /y=np.random.randn(500) /# target variable
> /ct_get = FunctionTransformer(*lambda *d:d[0:10]) /# transformer to 
> extract continuous variables
> /dt_get = FunctionTransformer(*lambda *d:d[11:20]) /# transformer to 
> extract discrete variables
>
> # first step is a regression pipeline
> /reg = 
> Pipeline([(*'ct_vars'*,ct_get),(*'scaler'*,StandardScaler()),(*'poly'*,PolynomialFeatures(degree=3)),(*'lasso'*,Lasso())])
> /# A random forest feeds on the discrete part of the data + one 
> continuous variable
> /estimator = 
> Pipeline([(*'level1'*,FeatureUnion([(*'dt_vars'*,dt_get),(*'reg'*,reg)])),(*'rf'*,RandomForestRegressor())])
>
> estimator.fit(X,y)
> *print **"R^2 score is :"*,estimator.score(X,y)
>
> *Augustin LEFEVRE*|**Consultant Senior |Ykems| -
>
>  M : +33 7 77 97 94 89 | alefevre at ykems.com 
> <mailto:alefevre at ykems.com>| www.ykems.com <http://www.ykems.com/>
>
> https://www.beijaflore.com/_mailing/signature/image001.png 
> <http://www.linkedin.com/company/beijaflore?trk=top_nav_home>https://www.beijaflore.com/_mailing/signature/image002.png 
> <https://twitter.com/BeijafloreGroup>https://www.beijaflore.com/_mailing/signature/image003.png 
> <https://www.facebook.com/BeijafloreGroup>https://www.beijaflore.com/_mailing/signature/image004.png 
> <https://www.youtube.com/user/ComBeijaflore>
>
> PSave a tree ! Think before you print
>
> /SECURE BUSINESS/
>
> /This message and its attachment contain information that may be 
> privileged or confidential and is the property of Beijaflore. It is 
> intended only for the person to whom it is addressed. If you are not 
> the intended recipient, you are not authorized to read, print, retain, 
> copy, disseminate, distribute, use or rely on the information 
> contained in this email. If you receive this message in error, please 
> notify the sender immediately and delete all copies of this message./
>
>
>
> _______________________________________________
> scikit-learn mailing list
> scikit-learn at python.org
> https://mail.python.org/mailman/listinfo/scikit-learn

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170906/7941f2fd/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 601 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170906/7941f2fd/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 390 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170906/7941f2fd/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 1422 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170906/7941f2fd/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 887 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scikit-learn/attachments/20170906/7941f2fd/attachment-0007.png>


More information about the scikit-learn mailing list