简体   繁体   English

带有大量参数的 sklearn 的自定义估计器

[英]Custom estimator for sklearn with a lot of params

I am trying to make custom estimator for sklearn with a lot of params.我正在尝试使用很多参数为 sklearn 制作自定义估算器。 As i understand, i should do something like this:据我了解,我应该做这样的事情:

    class MyEstim(BaseEstimator):

     def __init__(self, param1, param2):
      super().__init__()

     self.param1 = param1
     self.param2 = param2
     ...

But i am not sure how to do it if i have tens or hundreds of params.但是如果我有数十或数百个参数,我不知道该怎么做。 It is not a problem to build params - valid values list, but i dont understand how it will be better to pass this list to estimator in order to make it working in sklearn.构建参数 - 有效值列表不是问题,但我不明白将这个列表传递给 estimator 以使其在 sklearn 中工作如何更好。

Something like this not working for me:这样的事情对我不起作用:

         def __init__(self, **kwargs):
          super().__init__()
          # init params from kwargs

Because, as i understand, there are some conventions in sklearn code, so it can only initialize params which are explicitly defined in __init___ function.因为,据我所知,sklearn 代码中有一些约定,所以它只能初始化在__init___函数中明确定义的参数。

You can use **kwargs as long as your get_params and set_params methods work as expected.只要您的get_paramsset_params方法按预期工作,您就可以使用**kwargs

Example:例子:

(taken from a blog post I authored) (摘自我撰写的博客文章


class LoggingEstimator(BaseEstimator):
    def __init__(self, est_class=LinearRegression, **kwargs):

        self.est_class = est_class

        # kwargs depend on the model used, so assign them whatever they are
        for key, value in kwargs.items():
            setattr(self, key, value)

        self._param_names = ['est_class'] + list(kwargs.keys())

    def get_params(self, deep=True):
        # Note: we are ignoring the deep parameter
        # this will not work with estimators that have sub-estimators
        # see https://scikit-learn.org/stable/developers/develop.html#get-params-and-set-params
        return {param: getattr(self, param)
                for param in self._param_names}

    def set_params(self, **parameters):
        for parameter, value in parameters.items():
            setattr(self, parameter, value)

        return self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sklearn Pipeline - 如何在自定义Transformer(而不是Estimator)中继承get_params - Sklearn Pipeline - How to inherit get_params in custom Transformer (not Estimator) 来自 statsmodels 的自定义估算器 WLS 的 sklearn check_estimator 错误 - sklearn check_estimator error for a custom estimator WLS from statsmodels 如何在 sklearn 中将 GridSearchCV 与自定义估计器一起使用? - how to use GridSearchCV with custom estimator in sklearn? 关于使用 RandomSearchCV 和使用 sklearn 的自定义估计器的问题? - Questions on using RandomSearchCV with custom estimator using sklearn? sklearn AssertionError:不等于自定义估算器的容差 - sklearn AssertionError: not equal to tolerance on custom estimator 无法加载腌制的自定义估算器 sklearn 管道 - Unable to load pickled custom estimator sklearn pipeline 包含估算器的sklearn会导致get_params缺失自我错误 - sklearn wrapping an estimator causes error on get_params missing self 如何在 sklearn 中使用自定义估计器进行交叉验证? - How to use cross-validation with custom estimator in sklearn? 如何在sklearn中编写自定义估算器并对其进行交叉验证? - How to write a custom estimator in sklearn and use cross-validation on it? 在 Sklearn 中的自定义估计器的交叉验证中打印语句 - Print statement in cross-validation on custom estimator in Sklearn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM