Max Power Max Power - 1 year ago 287
Python Question

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

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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download