我正在尝试为带有很多参数的sklearn定制估计器。据我了解,我应该做这样的事情:
class MyEstim(BaseEstimator):
def __init__(self, param1, param2):
super().__init__()
self.param1 = param1
self.param2 = param2
...
但是如果我有几十个或几百个参数,我不确定该怎么做。构建参数-有效值列表不是问题,但是我不明白将这个列表传递给估计器以使其在sklearn中工作会更好。
类似这样的东西对我不起作用:
def __init__(self, **kwargs):
super().__init__()
# init params from kwargs
因为据我所知,sklearn代码中有一些约定,所以它只能初始化__init___
函数中显式定义的参数。
答案 0 :(得分:0)
只要您的**kwargs
和get_params
方法可以正常工作,就可以使用set_params
。
示例:
(摘自我创作的blog post)
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