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

使用XGBoostRegressor搭配GridSearchCV出现参数未使用警告如何解决

警告触发原因

eval_set和verbose是XGBRegressor的fit()方法专属参数,不属于类初始化构造函数的合法入参。你将这两个参数放在XGBRegressor()初始化阶段传入,XGBoost核心逻辑无法在构造阶段识别这两个参数,就会触发「参数可能未被使用」的警告。

解决方案

调整参数的传入位置,将eval_set和verbose移到GridSearchCV的fit()方法中传入即可,修改后的代码如下:

# XGBoostRegressor
parameters = {
    'n_estimators': [100, 200, 300, 400],
    'learning_rate': [0.001, 0.005, 0.01, 0.05],
    'max_depth': [8, 10, 12, 15],
    'gamma': [0.001, 0.005, 0.01, 0.02],
    'random_state': [42]
}

eval_set = [(X_train, y_train), (X_valid, y_valid)]
# 初始化阶段仅传入构造函数支持的参数
model = xgb.XGBRegressor(objective = 'reg:squarederror')
clf = GridSearchCV(model, parameters)

# fit阶段传入fit方法专属参数
clf.fit(X_train, y_train, eval_set=eval_set, verbose=False)

print(f'Best params: {clf.best_params_}')
print(f'Best validation score = {clf.best_score_}')

如果临时需要屏蔽该类警告(不推荐,优先调整参数位置),可以在代码开头加入警告过滤逻辑:

import warnings
warnings.filterwarnings('ignore', category=UserWarning, message='Parameters: { "eval_set", "verbose" } might not be used.')

内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 05:45:03