我目前正在尝试使用 BayesianOptimization 优化 XGBRegressor。这是代码:

I am currently trying to optimize an XGBRegressor using the BayesianOptimization. Here is the code :

我想对我的数据集应用回归。我目前正在尝试使用 BayesianOptimization 优化 XGBRegressor,但每次 运行 我都会遇到同样的错误。我对机器赚钱不是很熟悉,所以如果能得到任何帮助,我将不胜感激。这是代码:

optimizer = BayesianOptimization(f=xgboostcv,
                     domain=params,
                     model_type='GP',
                     acquisition_type='EI',
                     acquisition_jitter=0.05,
                     exact_feval=True,
                     maximize=True,
                     verbosity=True) 
optimizer.run_optimization(max_iter=20,verbosity=True)

这里是错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-27-921c3885d33d> in <module>
----> 1 optimizer = BayesianOptimization(f=xgboostcv,
  2                                  domain=params,
  3                                  model_type='GP',
  4                                  acquisition_type='EI',
  5                                  acquisition_jitter=0.05,

~\anaconda3\envs\sklearn\lib\site-packages\GPyOpt\methods\bayesian_optimization.py in 
__init__(self, f, domain, constraints, cost_withGradients, model_type, X, Y, 
initial_design_numdata, initial_design_type, acquisition_type, normalize_Y, 
exact_feval, acquisition_optimizer_type, model_update_interval, evaluator_type, 
batch_size, num_cores, verbosity, verbosity_model, maximize, de_duplication, **kwargs)
   92         self.constraints = constraints
   93         self.domain = domain
---> 94         self.space = Design_space(self.domain, self.constraints)
   95 
   96         # --- CHOOSE objective function

~\anaconda3\envs\sklearn\lib\site-packages\GPyOpt\core\task\space.py in 
__init__(self, space, constraints, store_noncontinuous)
   69 
   70         ## --- Transform input config space into the objects used to run the 
optimization
---> 71         self._translate_space(self.config_space)
   72         self._expand_space()
   73         self._compute_variables_indices()

~\anaconda3\envs\sklearn\lib\site-packages\GPyOpt\core\task\space.py in 
_translate_space(self, space)
   177         for i, d in enumerate(space):
   178             descriptor = deepcopy(d)
--> 179             descriptor['name'] = descriptor.get('name', 'var_' + str(i))
 180             descriptor['type'] = descriptor.get('type', 'continuous')
 181             if 'domain' not in descriptor:

 AttributeError: 'str' object has no attribute 'get'

以下是“params”对象规范:

params ={'max_depth': (2, 5),
     'learning_rate': (0.01, 0.3),
     'n_estimators': (1000, 2500),
     'gamma': (1., 0.01),
     'min_child_weight': (1, 10),
     'max_delta_step': (0, 0.1),
     'subsample': (0.5, 0.8),
     'colsample_bytree' :(0.1, 0.99),
     'reg_alpha':(0.1, 0.5), 
     'reg_lambda':(0.1, 0.9)
}

GPyOpt 包以比其他流行的搜索方法更详细(因此更灵活?)的方式指定超参数 space。文档中有示例: https://gpyopt.readthedocs.io/en/latest/GPyOpt.core.task.html#GPyOpt.core.task.space.Design_space

space = [
    {'name': 'max_depth', 'type': 'discrete', 'domain': (2,3,4,5)},
    {'name': 'learning_rate', 'type': 'continuous', 'domain': (0.01, 0.3)},
    ...
]

在这里重要的是,域需要是一个 list 的字典。在回溯中,您可以看到代码在域上使用了 enumerate,并且枚举一个字典只是枚举键,因此在它期望字典的地方抱怨字符串。