【发布时间】:2023-01-01 01:05:26
【问题描述】:
我的问题是我正在尝试调整 RandomForestRegressor。在使用默认参数进行交叉验证和训练测试拆分方法时,我得到的 R2 分数约为 0.85 到 0.90 当我尝试使用 GridSearchCV 找到最佳参数时,我得到了大约 0.60 到 0.62 的最佳 R2。请注意,我也在我的网格中包含了我的默认参数。 (所以如果默认是最好的设置,它应该在那里)
我尝试了很多方法,比如更改网格值(并将默认参数包括回网格)。但它甚至没有接近给我带来的默认设置。
请在此处找到代码
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)
# evaluate the model using cross-validation
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']]))
这给出了一个R2 分数为 0.85 至 0.90为了进一步改进这一点,我选择使用以下基本参数进行超参数调整
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]}
接下来是:
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_)
我在这里得到的最好成绩是0.6067.
P.S 请注意,所有这些 None 都是这些参数的默认设置。
谢谢!!
【问题讨论】:
-
model__max_depth在parameters中包含两次。
标签: python scikit-learn regression random-forest gridsearchcv