Sklearn pipe - Applying sample weights after applying a polynomial feature transformation in a pipeline

I want to apply sample weights and at the same time use a conveyor from sklearn, which should do a function conversion, for example. polynomial, and then apply a regressor, for example. ExtraTrees.

I use the following packages in the two examples below:

from sklearn.ensemble import ExtraTreesRegressor import numpy as np from sklearn.pipeline import Pipeline from sklearn.preprocessing import PolynomialFeatures 

Everything works well, while I separately transform the functions, then create and equip the model:

 #Feature generation X = np.random.rand(200,4) Y = np.random.rand(200) #Feature transformation poly = PolynomialFeatures(degree=2) poly.fit_transform(X) #Model generation and fit clf = ExtraTreesRegressor(n_estimators=5, max_depth = 3) weights = [1]*100 + [2]*100 clf.fit(X,Y, weights) 

But doing this in the pipeline does not work:

 #Pipeline generation pipe = Pipeline([('poly2', PolynomialFeatures(degree=2)), ('ExtraTrees', ExtraTreesRegressor(n_estimators=5, max_depth = 3))]) #Feature generation X = np.random.rand(200,4) Y = np.random.rand(200) #Fitting model clf = pipe weights = [1]*100 + [2]*100 clf.fit(X,Y, weights) 

I get the following error: TypeError: fit () takes no more than 3 arguments (4 data) In this simple example, I don’t need to change the code, but when I want to run several different tests on my real data in my real code, being able to use pipelines and sample weight

+6
source share
1 answer

The fit Pipeline method mentions **fit_params . You must specify at what stage of the pipeline you want to apply the parameter. You can achieve this by following the naming conventions in the docs:

To do this, it allows you to set the parameters of the various stages, using their names and the parameter name, separated by the symbol "__", as in the example below.

So, all of the above, try changing the last line to:

 clf.fit(X,Y, **{'ExtraTrees__sample_weight': weights}) 

This is a good example of how to work with parameters in pipelines.

+8
source

All Articles