from sklearn.model_selection import GridSearchCV, RandomizedSearchCV # 这里对随机森林模型进行参数搜索 param_grid = [ {'n_estimators': [80, 100, 120], 'max_features': [2, 6, 10]}, {'bootstrap': [False], 'n_estimators': [80, 100, 120], 'max_features': [2, 6, 10]} ] forest_reg = RandomForestRegressor() grid_search_forest = GridSearchCV(forest_reg, param_grid=param_grid, scoring='neg_mean_squared_error', cv=5) grid_search_forest.fit(housing_prepares, housing_label) print('网格搜索下最佳参数', grid_search_forest.best_params_) print('网格搜索下最佳评估器', grid_search_forest.best_estimator_) # 不同超参数下的模型得分 cvres = grid_search_forest.cv_results_ for mean_score, params in zip(cvres['mean_test_score'], cvres['params']): print(np.sqrt(-mean_score), params) 将以上代码用plot绘图
时间: 2024-03-22 14:42:27 浏览: 97
10,模型的优化-Live
抱歉,我作为语言模型无法绘图。但是,你可以将不同超参数下的模型得分绘制成图表进行可视化展示。可以使用 Matplotlib 或 Seaborn 来实现。以下是一个示例代码:
```python
import matplotlib.pyplot as plt
import pandas as pd
# 将 cv_results_ 转化为 DataFrame 格式
cvres_df = pd.DataFrame(grid_search_forest.cv_results_)
# 提取所需列
cvres_df = cvres_df[['param_n_estimators', 'param_max_features', 'mean_test_score']]
# 将 param_n_estimators 和 param_max_features 转化为数值类型
cvres_df['param_n_estimators'] = cvres_df['param_n_estimators'].astype('int')
cvres_df['param_max_features'] = cvres_df['param_max_features'].astype('int')
# 绘制图表
fig, ax = plt.subplots(figsize=(12, 8))
for n_estimators in [80, 100, 120]:
df = cvres_df[cvres_df['param_n_estimators'] == n_estimators]
ax.plot(df['param_max_features'], np.sqrt(-df['mean_test_score']), label=f'n_estimators={n_estimators}')
ax.set_xlabel('max_features')
ax.set_ylabel('RMSE')
ax.legend()
plt.show()
```
该代码将不同超参数下的模型得分绘制成了一张折线图,横坐标为 max_features,纵坐标为 RMSE。其中,每种颜色代表不同的 n_estimators 值。你可以根据实际情况进行修改和调整。
阅读全文