如何结合MultiOutputRegressor与RandomizedSearchCV对GradientBoostingRegressor进行多输出回归的超参数调优?
如何结合MultiOutputRegressor与RandomizedSearchCV对GradientBoostingRegressor进行多输出回归的超参数调优?
嗨,这个问题我之前调参的时候也踩过坑!你现在遇到的报错原因很明确:你把GradientBoostingRegressor的参数直接传给了MultiOutputRegressor,但RandomizedSearchCV会默认把这些参数当成外层MultiOutputRegressor的参数,可它本身只有estimator和n_jobs这两个合法参数,自然会提示参数无效啦。
解决方法其实很简单——scikit-learn里对于这种嵌套模型(外层包装器+内层estimator),调参时需要用**双下划线__**来指定参数属于内层模型。具体来说,就是给你参数字典里的每个键都加上estimator__前缀,让RandomizedSearchCV知道这些参数是传给里面的GradientBoostingRegressor的。
修改后的代码如下:
gradboost_n_estimators = [100, 200, 300, 400, 500, 600, 700, 800] gradboost_max_depth = [3, 4, 5, 6, 7, 8] gradboost_eta = [0.01, 0.02, 0.04, 0.08, 0.012, 0.16] gradboost_max_features = ["sqrt", "log2", None] gradboost_alpha = [0.1, 0.2, 0.4, 0.6, 0.8, 0.9] gradboost_subsample = [0.5, 0.6, 0.7, 0.8, 0.9, 1] gradboost_loss = ["squared_error", "absolute_error", "huber", "quantile"] # 给每个参数键加上estimator__前缀 gradboost_dict_params = { "estimator__n_estimators": gradboost_n_estimators , "estimator__max_depth": gradboost_max_depth , "estimator__learning_rate": gradboost_eta , "estimator__max_features": gradboost_max_features , "estimator__alpha": gradboost_alpha , "estimator__subsample": gradboost_subsample , "estimator__loss": gradboost_loss } model_gradboost = MultiOutputRegressor(GradientBoostingRegressor(random_state=42)) random_search = RandomizedSearchCV(model_gradboost, param_distributions=gradboost_dict_params, n_iter=200, cv=5, scoring='r2', return_train_score=True) random_search.fit(X_train, y_train)
这样调整之后,RandomizedSearchCV就能正确识别这些参数是属于内层的GradientBoostingRegressor,不会再报错啦。另外提一句,之后你查看最优参数的时候,结果里的参数名也会带着estimator__前缀,这是正常的,代表这些是内层模型的参数。
备注:内容来源于stack exchange,提问作者Cesinco
相关产品推荐
相关产品推荐

