程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Typeerror: float object is not subscribable is reported in parameter adjustment of xgboost algorithm based on Python Bayesian optimization

編輯:Python

be based on Python Bayesian optimization XGBoost The operation of algorithm parameter adjustment is as follows :

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

Report the following error :

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

Reference key codes are as follows :

def _xgb_logistic_evaluate(max_depth, subsample, gamma, colsample_bytree, min_child_weight):
import xgboost as xgb
params = {

'objective': 'binary:logistic', # The problem of logistic regression dichotomy 
'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, # Objective function 
pbounds=pbounds, # Value space 
verbose=2, # verbose = 2 Print all when ,verbose = 1 Print the maximum value found in the run ,verbose = 0 Will print nothing 
random_state=1,
)
bo.maximize(init_points=init_points, # Number of steps of random search 
n_iter=n_iter, # Number of Bayesian Optimization iterations performed 
acq='ei')
print(bo.max)
res = bo.max
params_max = res['params']
return params_max

Reference resources stackoverflow Explanation above :

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

therefore , Uninstall current scipy 1.8.1, Return to 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

Successfully run the Bayesian optimization parameter adjuster again .

Reference resources :

seul233. python When using Bayesian to optimize random forest TypeError: ‘float’ object is not subscriptable. CSDN Blog . 2022.03

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved