基於Python貝葉斯優化XGBoost算法調參運行情況如下:
報出如下錯誤:
Traceback (most recent call last):
......
suggestion = acq_max(
File "/usr/local/python3/lib/python3.8/site-packages/bayes_opt/util.py", line 65, in acq_max
if max_acq is None or -res.fun[0] >= max_acq:
TypeError: 'float' object is not subscriptable
參考關鍵代碼如下:
def _xgb_logistic_evaluate(max_depth, subsample, gamma, colsample_bytree, min_child_weight):
import xgboost as xgb
params = {
'objective': 'binary:logistic', # 邏輯回歸二分類的問題
'eval_metric': 'auc',
'max_depth': int(max_depth),
'subsample': subsample, # 0.8
'eta': 0.3,
'gamma': gamma,
'colsample_bytree': colsample_bytree,
'min_child_weight': min_child_weight}
cv_result = xgb.cv(params, self.dtrain,
num_boost_round=30, nfold=5)
return 1.0 * cv_result['test-auc-mean'].iloc[-1]
def evaluate(self, bo_f, pbounds, init_points, n_iter):
bo = BayesianOptimization(
f=bo_f, # 目標函數
pbounds=pbounds, # 取值空間
verbose=2, # verbose = 2 時打印全部,verbose = 1 時打印運行中發現的最大值,verbose = 0 將什麼都不打印
random_state=1,
)
bo.maximize(init_points=init_points, # 隨機搜索的步數
n_iter=n_iter, # 執行貝葉斯優化迭代次數
acq='ei')
print(bo.max)
res = bo.max
params_max = res['params']
return params_max
參考stackoverflow上的解釋:
This is related to a change in scipy 1.8.0, One should use -np.squeeze(res.fun) instead of -res.fun[0]
https://github.com/fmfn/BayesianOptimization/issues/300
The comments in the bug report indicate reverting to scipy 1.7.0 fixes this,
UPDATED: It seems the fix has been merged in the BayesianOptimization package, but the new maintainer is unable to push a release to pypi https://github.com/fmfn/BayesianOptimization/issues/300#issuecomment-1146903850
因此,卸載當前scipy 1.8.1,退回到scipy 1.7.0。
[[email protected] bin]# pip3 uninstall scipy
......
Successfully uninstalled scipy-1.8.1
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple scipy==1.7
Successfully installed scipy-1.7.0
成功再運行貝葉斯優化調參程序。
參考:
seul233. python使用貝葉斯優化隨機森林時出現TypeError: ‘float’ object is not subscriptable. CSDN博客. 2022.03
https://stackoverflow.com/questions/71460894/bayesianoptimization-fails-due-to-float-error
With the rapid development of
One . brief introduction DeepS