You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GridSearch/RandomizedSearchCV的best_estimator打印报错求助

解决XGBoost RandomizedSearchCV的KeyError: 'base_score'问题

嘿,我来帮你搞定这个KeyError: 'base_score'的问题——这其实是XGBoost和scikit-learn版本不兼容导致的小坑,同时你代码里还有个小拼写错误(不过这不是报错核心),下面给你一步步拆解解决方案。

问题背景

你在拟合模型后尝试打印RandomizedSearchCV的best_estimator_属性时触发了KeyError,而且换用GridSearchCV或其他数据集也会出现相同问题,训练时还收到了参数验证的提示,这些都是版本兼容冲突的典型表现。

核心代码与报错详情

核心代码

from xgboost.sklearn import XGBRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
import time  # 注意你代码里漏了time导入,这里补上

parameters = { 'min_child_weight':[2,3,4], }
xlf = XGBRegressor(learning_rate=0.1,n_estimators=50,max_depth=5,min_child_weight = 1, subsample=0.8,colsample_btree=0.8,objective='reg:linear', scale_pos_weight=1,random_state=27)
n_iter_search = 5
gsearch = RandomizedSearchCV(xlf,param_distributions=parameters,n_iter=n_iter_search, cv=2, iid=False)
start=time.clock()
gsearch.fit(x_train.values,y_train.values,eval_set = [(x_test.values,y_test.values)],eval_metric = "rmse", early_stopping_rounds = 20)
end=time.clock()
print('RandomSearch Running time: %s Seconds'%(end-start))
print("Best score: %0.3f" % gsearch.best_score_)
best_estimator = gsearch.best_estimator_
print("Best parameters set",best_estimator)

报错信息

Traceback (most recent call last):
  File "D:\PythonProject\TestPackagePytorch\code.py", line 213, in <module>
    print("Best parameters set",best_estimator)
  File "E:\Anaconda3\envs\tensorflow\lib\site-packages\sklearn\base.py", line 279, in __repr__
    repr_ = pp.pformat(self)
  File "E:\Anaconda3\envs\tensorflow\lib\pprint.py", line 144, in pformat
    self._format(object, sio, 0, 0, {}, 0)
  File "E:\Anaconda3\envs\tensorflow\lib\pprint.py", line 161, in _format
    rep = self._repr(object, context, level)
  File "E:\Anaconda3\envs\tensorflow\lib\pprint.py", line 393, in _repr
    self._depth, level)
  File "E:\Anaconda3\envs\tensorflow\lib\site-packages\sklearn\utils\_pprint.py", line 170, in format
    changed_only=self._changed_only)
  File "E:\Anaconda3\envs\tensorflow\lib\site-packages\sklearn\utils\_pprint.py", line 414, in _safe_repr
    params = _changed_params(object)
  File "E:\Anaconda3\envs\tensorflow\lib\site-packages\sklearn\utils\_pprint.py", line 98, in _changed_params
    if (repr(v) != repr(init_params[k]) and
KeyError: 'base_score'

训练提示

This may not be accurate due to some parameters are only used in language bindings but passed down to XGBoost core. Or some parameters are not used but slip through this verification. Please open an issue if you find above cases.

解决方案

这里有几个可行的解决办法,按优先级推荐:

  1. 升级到兼容的库版本(最根本)
    这个错误的本质是scikit-learn的参数检查逻辑和XGBoost的内部参数初始化不匹配,升级到兼容版本就能直接解决。推荐的稳定组合:

    • XGBoost 1.6.x + scikit-learn 1.0.x
    • 或者XGBoost 2.x + scikit-learn 1.2+
      执行命令升级:
    pip install --upgrade xgboost scikit-learn
    
  2. 绕过直接打印estimator对象
    如果暂时不能升级,可以不直接打印整个best_estimator_,而是提取参数:

    # 替换原来的print语句
    print("Best parameters set:", gsearch.best_params_)
    # 如果需要查看完整参数,手动过滤问题键
    full_params = best_estimator.get_params()
    if 'base_score' in full_params:
        del full_params['base_score']
    print("Full estimator parameters:", full_params)
    
  3. 修正代码中的拼写错误
    你代码里的colsample_btree应该是colsample_bytree,虽然这不是报错的原因,但会影响模型性能,建议修正。

  4. 换用XGBoost原生调参工具
    如果上述方法都不行,可以考虑用XGBoost自带的cv函数,或者optuna这类第三方调参库,避开scikit-learn的兼容性问题。

验证

修改后重新运行代码,如果不再出现KeyError: 'base_score',且能正常输出最佳参数,就说明问题解决了。

内容的提问来源于stack exchange,提问作者余九天

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:32:37