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

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 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析