seul233 2022-03-31 15:28 采纳率: 0%
浏览 1324

python使用贝叶斯优化随机森林时出现TypeError: 'float' object is not subscriptable

问题遇到的现象和发生背景

在对随机森林调参的时候,出现了TypeError: 'float' object is not subscriptable

问题相关代码,请勿粘贴截图

代码如下

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
import matplotlib.pyplot as plt
data =  pd.read_csv("x.csv")
y =  pd.read_csv("y.csv")
feature = ['surface area(approx)','surface area(grid)','volume','hydration energy(kcal/mol)','logP','refractivity','polarizability','mass']
x = data[feature]
y = y['pIC50']
train_x, test_x, train_y, test_y = train_test_split(x, y,random_state=0,test_size=7)
model = RandomForestRegressor()
model = model.fit(train_x, train_y)
score = model.score(test_x, test_y)
print('默认参数下测试集评分:')
print(score)
def black_box_function(n_estimators, min_samples_split, max_features, max_depth):
    res = RandomForestRegressor(n_estimators=int(n_estimators),
                                min_samples_split=int(min_samples_split),
                                max_features=min(max_features, 0.999),  # float
                                max_depth=int(max_depth),
                                random_state=0
                                ).fit(train_x, train_y).score(test_x, test_y)
    return res
#确定域空间
pbounds= {'n_estimators': (1, 250),
         'min_samples_split': (2, 25),
         'max_features': (0.1, 0.999),
         'max_depth': (5, 25)}
from bayes_opt import BayesianOptimization
optimizer = BayesianOptimization(
        f=black_box_function,
        pbounds=pbounds,
        verbose=2,  # verbose = 1 prints only when a maximum is observed, verbose = 0 is silent
        random_state=0,
    )
optimizer.maximize(
        init_points=5,  #执行随机搜索的步数
        n_iter=25,   #执行贝叶斯优化的步数
    )
运行结果及报错内容
|   iter    |  target   | max_depth | max_fe... | min_sa... | n_esti... |
|  1        | -0.8974   |  15.98    |  0.743    |  15.86    |  136.7    |
|  2        | -0.7852   |  13.47    |  0.6807   |  12.06    |  223.1    |
|  3        | -1.096    |  24.27    |  0.4447   |  20.21    |  132.7    |
|  4        | -1.25     |  16.36    |  0.9321   |  3.634    |  22.7     |
|  5        | -1.08     |  5.404    |  0.8485   |  19.9     |  217.6    |
StopIteration                             Traceback (most recent call last)
File D:\python\lib\site-packages\bayes_opt\bayesian_optimization.py:179, in BayesianOptimization.maximize(self, init_points, n_iter, acq, kappa, kappa_decay, kappa_decay_delay, xi, **gp_params)
    178 try:
--> 179     x_probe = next(self._queue)
    180 except StopIteration:

File D:\python\lib\site-packages\bayes_opt\bayesian_optimization.py:25, in Queue.__next__(self)
     24 if self.empty:
---> 25     raise StopIteration("Queue is empty, no more objects to retrieve.")
     26 obj = self._queue[0]

StopIteration: Queue is empty, no more objects to retrieve.

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 optimizer.maximize(
      2         init_points=5,  #执行随机搜索的步数
      3         n_iter=25,   #执行贝叶斯优化的步数
      4     )

File D:\python\lib\site-packages\bayes_opt\bayesian_optimization.py:182, in BayesianOptimization.maximize(self, init_points, n_iter, acq, kappa, kappa_decay, kappa_decay_delay, xi, **gp_params)
    180 except StopIteration:
    181     util.update_params()
--> 182     x_probe = self.suggest(util)
    183     iteration += 1
    185 self.probe(x_probe, lazy=False)

File D:\python\lib\site-packages\bayes_opt\bayesian_optimization.py:131, in BayesianOptimization.suggest(self, utility_function)
    128     self._gp.fit(self._space.params, self._space.target)
    130 # Finding argmax of the acquisition function.
--> 131 suggestion = acq_max(
    132     ac=utility_function.utility,
    133     gp=self._gp,
    134     y_max=self._space.target.max(),
    135     bounds=self._space.bounds,
    136     random_state=self._random_state
    137 )
    139 return self._space.array_to_params(suggestion)

File D:\python\lib\site-packages\bayes_opt\util.py:65, in acq_max(ac, gp, y_max, bounds, random_state, n_warmup, n_iter)
     62     continue
     64 # Store it if better than previous minimum(maximum).
---> 65 if max_acq is None or -res.fun[0] >= max_acq:
     66     x_max = res.x
     67     max_acq = -res.fun[0]

TypeError: 'float' object is not subscriptable


我的解答思路和尝试过的方法

只出现了5组数据

我想要达到的结果
  • 写回答

7条回答 默认 最新

  • weixin_48291099 2022-04-12 16:44
    关注

    请问楼主解决了吗?一模一样的问题

    评论

报告相同问题?

问题事件

  • 创建了问题 3月31日

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致