首页 > 解决方案 > 使用网格搜索调整了 3 个参数,但 best_estimator_ 只有 2 个参数

问题描述

我正在使用管道和网格搜索调整梯度增强分类器

我的管道是

pipe = make_pipeline(StandardScaler(with_std=True, with_mean=True), \
    RFE(RandomForestClassifier(), n_features_to_select= 15), \
        GradientBoostingClassifier(random_state=42, verbose=True))

参数 gr 是:

    tuned_parameters = [{'gradientboostingclassifier__max_depth': range(3, 5),\
'gradientboostingclassifier__min_samples_split': range(4,6),\
'gradientboostingclassifier__learning_rate':np.linspace(0.1, 1, 10)}]

网格搜索完成为

grid = GridSearchCV(pipe, tuned_parameters, cv=5, scoring='accuracy', refit=True)
grid.fit(X_train, y_train)

在训练数据中拟合模型后,当我检查时,grid.best_estimator我只能找到learning_rate and min_samples_split我正在拟合的 2 个参数()。我没有max_depth在最佳估计器中找到参数。

grid.best_estimator_.named_steps['gradientboostingclassifier'] =

GradientBoostingClassifier(learning_rate=0.9, min_samples_split=5,
                           random_state=42, verbose=True)

但是,如果我使用grid.cv_results来找到最好的 ' mean_test_score' 并找到该测试分数的相应参数,那么我可以max_depth在其中找到 。

inde = np.where(grid.cv_results_['mean_test_score'] == max(grid.cv_results_['mean_test_score']))

    grid.cv_results_['params'][inde[-1][0]]
{'gradientboostingclas...rning_rate': 0.9, 'gradientboostingclas..._max_depth': 3, 'gradientboostingclas...ples_split': 5}
special variables
function variables
'gradientboostingclassifier__learning_rate':0.9
'gradientboostingclassifier__max_depth':3
'gradientboostingclassifier__min_samples_split':5

我现在的疑问是,如果我使用训练有素的管道(在我的情况下,对象的名称是“网格”)它还会使用“ max_depth”参数还是不会?那么是不是更好使用' best parameters'这给了我最好的' mean_test_score'取自grid.cv_results

标签: pythonscikit-learnpipelinecross-validationgrid-search

解决方案


您的管道已针对您指定的所有三个参数进行了调整。只是最佳值max_depth恰好是默认值。打印分类器时,将不包含默认值。比较以下输出:

print(GradientBoostingClassifier(max_depth=3)) # default
# output: GradientBoostingClassifier()

print(GradientBoostingClassifier(max_depth=5)) # not default
# output: GradientBoostingClassifier(max_depth=5)

best_params_通常,最佳实践是通过拟合对象的属性访问最佳参数,GridSearchCV因为这将始终包括所有参数:

grid.best_params_

推荐阅读