首页 > 解决方案 > 为 XGBoost 调整超参数时出现 BayesSearchCV 错误。ValueError:并非所有点都在空间范围内

问题描述

我正在尝试使用来自 Scikit Optimizer 的 BayesSearchCV 优化 XGBoost 模型,这是我尝试使用的代码:

from skopt import BayesSearchCV
import xgboost as xgb
from main import format_data_for_xgboost

x_train, x_test, y_train, y_test = format_data_for_xgboost() # function in sep script

opt = BayesSearchCV(
    xgb.XGBRegressor(objective='reg:squarederror', n_jobs=4),
    {
        'n_estimators': (1, 50),
        'max_depth': (1, 20),
        'learning_rate': (10**-5, 10**0, "log-uniform"),
        'min_child_weight': (1, 5),
        'max_delta_step': (1, 10)
    },
    n_iter=8,
    verbose=99
)

opt.fit(x_train, y_train)

它在前几次迭代中运行,分数从 -0.001 逐渐降低到 -0.009。

运行后:

[CV]  learning_rate=0, max_delta_step=7, max_depth=4, min_child_weight=5, n_estimators=46, score=-0.009, total=   0.1s

它错误:

ValueError: Not all points are within the bounds of the space.

我很确定这与“分数”有关,但是当我尝试手动设置分数时,它说它不能接受浮点数作为分数的参数。

我将不胜感激任何帮助理解如何克服这个错误。我不认为数据框有问题,因为我现在已经成功地将它们与 xgb.cv 和 xgbRegressor 一起使用,只是当我尝试使用贝叶斯优化时我开始遇到问题。

编辑:当我在 verbose=99 之后添加 score='neg_mean_squared_error' 作为参数时,它运行的时间更长,但之后我得到了同样的错误:

[CV]  learning_rate=0, max_delta_step=8, max_depth=4, min_child_weight=5, n_estimators=34, score=-2654.978, total=   0.1s

标签: pythonxgboosthyperparametersskoptbayessearchcv

解决方案


我自己在 XGBoost 上使用贝叶斯搜索遇到了这个问题。

我通过强制缩小它来“解决”这个问题。

  1. 将所有 CV 和迭代减少到 1 以加快训练时间。
  2. 注释掉一半的超参数范围。
  3. 火车。
  4. 如果skopt引发错误,则罪魁祸首在 #2 的注释行中。取消注释并缩小有问题的行。

必须有更好的方法来调试这个问题,但我发现这对我来说是最简单的。


推荐阅读