python - 使用网格搜索调整了 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
解决方案
您的管道已针对您指定的所有三个参数进行了调整。只是最佳值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_
推荐阅读
- node.js - 如何将环境变量从文件传递到节点命令
- python - 如何修复mysqlclient错误,无法下载,(wndows os)
- ios - 跟踪 iOS 应用的移动数据和 WiFi 使用情况
- aws-cloud9 - AWS-Cloud9 时间积累太快
- google-sheets - 如何使工作表过滤器仅在 G-Sheets 中显示带有多个变量的复选标记行
- google-cloud-automl - autoML 表是否支持多标签分类?
- typescript - 为什么 TypeScript 编译器重命名我的导入?
- sql - 如何将 DATE_TRUNC 转换为 MariaDB?
- docker - Traefik/Nextcloud 未获取远程 IP 地址
- excel - PowerBI 百分比平均值不同于 Excel 平均值