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

使用RandomizedSearchCV调优RegressorChain封装的XGBRegressor报参数无效错误

报错原因

scikit-learn 中针对嵌套封装的模型(例如 RegressorChain 挂载 base_estimator 的场景),调参时需要通过双下划线前缀指定参数所属的层级,否则搜索器会默认在最外层模型(此处为 RegressorChain)中查找参数,而 RegressorChain 本身不存在 n_estimators、max_depth 这类属于基模型 XGBRegressor 的参数,因此会抛出参数无效的报错。

解决方案

修改参数字典的键名,给所有属于 XGBRegressor 的参数加上 base_estimator__ 前缀(注意是两个连续下划线),修改后的代码如下:

# 调整后的参数网格
grid = {
        'base_estimator__n_estimators': [100, 500, 1000],
        'base_estimator__max_depth': [5, 10, 20, 30],
        'base_estimator__colsample_bytree': [0.8, 0.5], # 注:XGBoost 特征采样对应参数为 colsample_bytree,max_features 是 sklearn 原生树模型的参数,可根据实际使用的接口调整参数名
        'base_estimator__eta': [0.09, 0.1, 0.2],
        'base_estimator__booster': ["dart", "gblinear"]
        }

clf = XGBRegressor(objective='reg:squarederror')
chain = RegressorChain(base_estimator=clf, order=[0, 1, 2, 3, 4,5])

# Setup RandomizedSearchCV
rs_clf = RandomizedSearchCV(estimator=chain,
                            param_distributions=grid, 
                            n_iter=10, # number of models to try
                            cv=5,
                            verbose=1,
                            random_state=42,
                            refit=True) 

rs_clf.fit(X_train, y_train)

其余代码无需改动即可正常完成超参数搜索,如果后续需要调整 RegressorChain 本身的参数(比如 order、random_state 等),直接写原参数名即可,不需要加前缀。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 15:51:00