码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 基于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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    参考关键代码如下:

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    参考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
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    参考:

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

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

  • 相关阅读:
    Day03 -Git分支【概述与优点】
    《职场求生攻略》
    深度神经网络——什么是NLP(自然语言处理)?
    Diffie-Hellman协议中间人攻击方法及协议改进(网络空间安全实践与设计)
    Redis双写一致性、持久化机制、分布式锁
    2024三掌柜赠书活动第十六期:AI时代Python金融大数据分析实战
    《软件测试》实验四:移动应用自动化测试(安卓自动化测试)
    做纺织品进出口的都知道它的含金量--OEKO-TEX 100
    【python入门篇】条件表达式、循环(5)
    DSPE-PEG-Azide DSPE-PED-N3 磷脂-聚乙二醇-叠氮脂质PFG
  • 原文地址:https://blog.csdn.net/xiaoyw/article/details/125457194
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号