使用GridSearchCV调优RandomForestRegressor时R2分数低于基线问题
调优RandomForestRegressor时GridSearchCV得分远低于默认参数
我在调优RandomForestRegressor模型时遇到了问题:使用默认参数通过交叉验证和Train-Test Split方法得到的R2分数约为0.85至0.90,但用GridSearchCV寻找最佳参数时,得到的最佳R2分数仅为0.60至0.62。我已经将默认参数纳入参数网格中(确保如果默认设置是最优的,会被选中),但多次调整网格参数值后,结果仍远达不到默认设置的水平。
默认参数交叉验证代码
这段代码的R2分数为0.85至0.90:
my_steps = list() my_steps.append(('pt_transformer', PowerTransformer(method='box-cox'))) my_steps.append(('model', RandomForestRegressor())) pipeline = Pipeline(steps=steps) cv = KFold(n_splits=10, random_state=1, shuffle=True) # 用交叉验证评估模型 scores = cross_validate(pipeline, X, y, scoring=['r2', 'neg_mean_absolute_error'], cv=cv, n_jobs=-1) print('Average R2 test score: ', scores['test_r2'].mean()) print('Average MAE test score: ', np.mean([abs(s) for s in scores['test_neg_mean_absolute_error']]))
超参数调优代码
参数网格
所有None均为对应参数的默认设置:
parameters= {'model__max_depth' : [None, 50, 100, 150, 200], 'model__max_leaf_nodes': [None, 5, 10], 'model__max_samples': [None, 10, 20, 50], 'model__max_depth': [None, 100, 150]}
GridSearchCV执行代码
得到的最佳分数为0.6067:
grid = GridSearchCV(estimator = pipe, param_grid = parameters, cv = 5, scoring='r2', n_jobs = -1, verbose=3) grid.fit(X, y) print(grid.best_params_) print(grid.best_score_)
内容的提问来源于stack exchange,提问作者letdatado
相关产品推荐
相关产品推荐

