【问题标题】:Poorer R2 score than baseline when even default parameters are used in GridSearchCV for RandomForestRegressor当在 GridSearchCV 中为 RandomForestRegressor 使用默认参数时,R2 分数比基线差
【发布时间】: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_depthparameters 中包含两次。

标签: python scikit-learn regression random-forest gridsearchcv


【解决方案1】:

这些测量不同的东西:

KFold(n_splits=10)

GridSearchCV(cv=5)

第一个在训练期间看到 90% 的数据并得分为0.9。第二个在训练期间看到 80% 的数据并得分为0.6

如果交叉验证拆分的数量相同,结果应该更加一致。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-26
    • 2015-03-09
    • 2017-12-11
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    相关资源
    最近更新 更多