简体   繁体   English

Sklearn Pipeline - 如何在自定义Transformer(而不是Estimator)中继承get_params

[英]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.

相关问题 包含估算器的sklearn会导致get_params缺失自我错误 - sklearn wrapping an estimator causes error on get_params missing self Sklearn - 自动定义get_params() - Sklearn - define get_params() automatically mlflow 如何使用自定义转换器保存 sklearn 管道? - mlflow How to save a sklearn pipeline with custom transformer? 带有大量参数的 sklearn 的自定义估计器 - Custom estimator for sklearn with a lot of params 使用自定义变压器时如何正确腌制sklearn管道 - How to properly pickle sklearn pipeline when using custom transformer 无法克隆 object ' 似乎不是 scikit-learn 估算器,因为它没有实现 'get_params' 方法 - Cannot clone object ' does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods 无法加载腌制的自定义估算器 sklearn 管道 - Unable to load pickled custom estimator sklearn pipeline 自定义 function 变压器未按预期执行 - sklearn 管道 - Custom function transformer not performing as expected - sklearn pipeline 在sklearn中编写自定义转换器,该转换器在.transform中返回估算器的.predict - Write custom transformer in sklearn which returns .predict of estimator in .transform 如何在sklearn中保存自定义变换器? - How to save a custom transformer in sklearn?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM