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