[scikit-learn] using a predictor as transformer

Lefevre, Augustin alefevre at ykems.com
Wed Sep 6 11:56:32 EDT 2017

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())])

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>

P Save a tree ! Think before you print

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.

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

More information about the scikit-learn mailing list