IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 基于Python贝叶斯优化XGBoost算法调参报错“TypeError: ‘float‘ object is not subscriptable” -> 正文阅读

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

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

itertargetcolsam…gammamax_depthmin_ch…subsample
10.93980.80430.74836.0276.70.6514
20.94050.72310.26767.382347.60.7886
30.93880.80480.71676.818708.60.6096
40.94210.86760.47568.235155.30.6693
50.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,  # 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。

[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

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

参考:

seul233. python使用贝叶斯优化随机森林时出现TypeError: ‘float’ object is not subscriptable. CSDN博客. 2022.03

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

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-06-29 19:00:05  更:2022-06-29 19:04:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/26 12:13:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码