[英]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: 我有scikit-learn中的管道,它使用我定义的自定义变换器,如下所示:
class MyPipelineTransformer(TransformerMixin):
which defines functions 它定义了函数
__init__, fit() and transform()
However, when I use the pipeline inside RandomizedSearchCV, I get the following error: 但是,当我在RandomizedSearchCV中使用管道时,我收到以下错误:
'MyPipelineTransformer' object has no attribute 'get_params'
'MyPipelineTransformer'对象没有属性'get_params'
I've read online (eg links below) 我在线阅读(例如下面的链接)
(Python - sklearn) How to pass parameters to the customize ModelTransformer class by gridsearchcv (Python - sklearn)如何通过gridsearchcv将参数传递给自定义的ModelTransformer类
http://scikit-learn.org/stable/auto_examples/hetero_feature_union.html 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. 我可以通过继承BaseEstimator来获取'get_params',而不是仅仅继承TransformerMixin的当前代码。 But my transformer is not an estimator.
但我的变压器不是估算器。 Is there any downside to having a non-estimator inherit from BaseEstimator?
从BaseEstimator继承非估计量是否有任何不足之处? Or is that the recommended way to get get_params for any transformer (estimator or not) in a pipeline?
或者这是为管道中的任何变压器(估算器或非估算器)获取get_params的推荐方法吗?
Yes it looks like this is the standard way of achieving this. 是的,看起来这是实现这一目标的标准方法。 For example in the source for
sklearn.preprocessing
we have 例如,在
sklearn.preprocessing
的源代码中我们有
class FunctionTransformer(BaseEstimator, TransformerMixin)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.