基于Python贝叶斯优化XGBoost算法调参报错“TypeError: ‘float‘ object is not subscriptable”

基于Python贝叶斯优化XGBoost算法调参运行情况如下:

itertargetcolsam…gammamax_depthmin_ch…subsample10.93980.80430.74836.0276.70.651420.94050.72310.26767.382347.60.788630.93880.80480.71676.818708.60.609640.94210.86760.47568.235155.30.669350.93990.90020.97147.254569.20.9067

报出如下错误:

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,
        '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,
        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。

[root@DeepLearning bin]# pip3 uninstall scipy
......

  Successfully uninstalled scipy-1.8.1
[root@DeepLearning bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple scipy==1.7
  Successfully installed scipy-1.7.0

成功再运行贝叶斯优化调参程序。

参考:

https://stackoverflow.com/questions/71460894/bayesianoptimization-fails-due-to-float-error

Original: https://blog.csdn.net/xiaoyw/article/details/125457194
Author: 肖永威
Title: 基于Python贝叶斯优化XGBoost算法调参报错“TypeError: ‘float‘ object is not subscriptable”

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/31723/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部