Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sklearn Pipeline - How to inherit get_params in custom Transformer (not Estimator)

I have a pipeline in scikit-learn that uses a custom transformer I define like below:

class MyPipelineTransformer(TransformerMixin):

which defines functions

__init__, fit() and transform()

However, when I use the pipeline inside RandomizedSearchCV, I get the following error:

'MyPipelineTransformer' object has no attribute 'get_params'

I've read online (e.g. links below)

(Python - sklearn) How to pass parameters to the customize ModelTransformer class by gridsearchcv

http://scikit-learn.org/stable/auto_examples/hetero_feature_union.html

that I could get 'get_params' by inheriting from BaseEstimator, instead of my current code inheriting just from TransformerMixin. But my transformer is not an estimator. Is there any downside to having a non-estimator inherit from BaseEstimator? Or is that the recommended way to get get_params for any transformer (estimator or not) in a pipeline?

like image 601
Max Power Avatar asked Aug 23 '16 04:08

Max Power


1 Answers

Yes it looks like this is the standard way of achieving this. For example in the source for sklearn.preprocessing we have

class FunctionTransformer(BaseEstimator, TransformerMixin)
like image 124
maxymoo Avatar answered Oct 19 '22 20:10

maxymoo