优化代码 增加参数搜索的步长或者减少搜索范围 # GBDT 模型的网格搜索法 # 选择不同的参数 from sklearn.model_selection import GridSearchCV learning_rate_options = [0.01, 0.05, 0.1] max_depth_options = [3,5,7,9] n_estimators_options = [100, 300, 500] parameters = {'learning_rate':learning_rate_options, 'max_depth':max_depth_options, 'n_estimators':n_estimators_options} grid_gbdt = GridSearchCV(estimator= GradientBoostingClassifier(),param_grid=parameters,cv=10,scoring='accuracy') grid_gbdt.fit(X_train, y_train) # 结果输出 grid_gbdt.best_score_
时间: 2024-03-02 16:48:59 浏览: 103
可以尝试增加参数搜索的步长,比如将learning_rate_options改为[0.01, 0.05, 0.1, 0.2],或者将max_depth_options改为[3, 6, 9]。也可以减少搜索范围,比如将n_estimators_options改为[100, 200, 300]。这样可以在保证搜索精度的前提下,减少搜索时间。
修改后的代码示例:
```
from sklearn.model_selection import GridSearchCV
learning_rate_options = [0.01, 0.05, 0.1, 0.2]
max_depth_options = [3, 6, 9]
n_estimators_options = [100, 200, 300]
parameters = {'learning_rate':learning_rate_options,
'max_depth':max_depth_options,
'n_estimators':n_estimators_options}
grid_gbdt = GridSearchCV(estimator= GradientBoostingClassifier(),
param_grid=parameters,
cv=10,
scoring='accuracy')
grid_gbdt.fit(X_train, y_train)
# 结果输出
print("最优参数:", grid_gbdt.best_params_)
print("最优模型:", grid_gbdt.best_estimator_)
print("最优得分:", grid_gbdt.best_score_)
```
阅读全文