random_params = {'n_estimators': randint(low=80, high=121), 'max_features': randint(low=5, high=10)} forset_reg = RandomForestRegressor(random_state=2020) random_search_forest = RandomizedSearchCV(forest_reg, param_distributions=random_params, n_iter=20, scoring='neg_mean_squared_error', cv=15, random_state=2020) random_search_forest.fit(housing_prepares, housing_label) print('随机搜索下最佳参数', random_search_forest.best_params_) print('随机搜索下最佳评估器', random_search_forest.best_estimator_) final_model1 = random_search_forest.best_estimator_ y_predict = final_model1.predict(x_test) final_mse = mean_squared_error(y_test, y_predict) final_rmse = np.sqrt(final_mse) print(final_rmse) display(final_rmse) cvrus = random_search_forest.cv_results_ for mean_score, params in zip(cvrus['mean_test_score'], cvrus['params']): print(np.sqrt(-mean_score), params) 将以上代码绘制折线图实现
时间: 2024-01-18 14:03:48 浏览: 136
以下是将随机搜索下的不同参数组合的RMSE绘制成折线图的代码:
```python
import matplotlib.pyplot as plt
cvrus = random_search_forest.cv_results_
cv_rmse = [np.sqrt(-x) for x in cvrus['mean_test_score']]
params = [str(x) for x in cvrus['params']]
# 绘制RMSE折线图
plt.plot(params, cv_rmse, 'b.-')
plt.xlabel('Model Parameters')
plt.ylabel('RMSE')
plt.title('RMSE vs. Model Parameters')
plt.xticks(rotation=90)
plt.show()
```
这段代码将随机搜索下的不同参数组合的RMSE绘制成折线图,并且将模型参数作为x轴,RMSE作为y轴。通过旋转x轴上的参数标签(`plt.xticks(rotation=90)`),可以使得所有的标签都可读。
相关问题
from scipy.stats import randint random_params = {'n_estimators': randint(low=80, high=200), 'max_features': randint(low=2, high=8)} forset_reg = RandomForestRegressor(random_state=2020) random_search_forest = RandomizedSearchCV(forest_reg, param_distributions=random_params, n_iter=20, scoring='neg_mean_squared_error', cv=5, random_state=2020) random_search_forest.fit(housing_prepares, housing_label) cvrus = random_search_forest.cv_results_ for mean_score, params in zip(cvrus['mean_test_score'], cvrus['params']): print(np.sqrt(-mean_score), params) print('随机搜索下最佳参数', random_search_forest.best_params_) print('随机搜索下最佳评估器', random_search_forest.best_estimator_) 将以上代码用plot绘图
同样,我无法直接绘制图表,但是你可以将不同超参数下的模型得分绘制成图表进行可视化展示。以下是一个示例代码:
```python
import matplotlib.pyplot as plt
import pandas as pd
# 将 cv_results_ 转化为 DataFrame 格式
cvres_df = pd.DataFrame(random_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 range(80, 201, 20):
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 值。你可以根据实际情况进行修改和调整。
random_params = {'n_estimators': randint(low=80, high=121), 'max_features': randint(low=5, high=10)} forset_reg = RandomForestRegressor(random_state=2020) random_search_forest = RandomizedSearchCV(forest_reg, param_distributions=random_params, n_iter=20, scoring='neg_mean_squared_error', cv=15, random_state=2020) random_search_forest.fit(housing_prepares, housing_label) print('随机搜索下最佳参数', random_search_forest.best_params_) print('随机搜索下最佳评估器', random_search_forest.best_estimator_) final_model1 = random_search_forest.best_estimator_ y_predict = final_model1.predict(x_test) final_mse = mean_squared_error(y_test, y_predict) final_rmse = np.sqrt(final_mse) print(final_rmse) display(final_rmse) cvrus = random_search_forest.cv_results_ for mean_score, params in zip(cvrus['mean_test_score'], cvrus['params']): print(np.sqrt(-mean_score), params) 将以上代码绘图实现
很抱歉,我作为一个语言模型,无法直接绘图。但我可以提供一些思路,您可以使用 Matplotlib 或其他绘图库来实现。
根据您的代码,可以将随机搜索下不同参数组合的 RMSE 值绘制成折线图或柱状图,以便比较它们的效果。您可以使用 Matplotlib 库中的 plot() 函数或 bar() 函数来绘制图表。
首先,您需要将测试集的 RMSE 值与每个参数组合的 RMSE 值进行比较。您可以使用以下代码:
```
test_rmse = np.sqrt(mean_squared_error(y_test, y_predict))
cv_rmse = [np.sqrt(-x) for x in random_search_forest.cv_results_['mean_test_score']]
params = [str(x) for x in random_search_forest.cv_results_['params']]
cv_df = pd.DataFrame({'params': params, 'RMSE': cv_rmse})
cv_df = cv_df.sort_values(by='RMSE', ascending=True)
cv_df = cv_df.reset_index(drop=True)
cv_df.loc[len(cv_df)] = ['Test set', test_rmse]
```
然后,您可以使用以下代码将 RMSE 值绘制成折线图:
```
plt.plot(cv_df['params'], cv_df['RMSE'], label='CV RMSE')
plt.plot(cv_df['params'], cv_df[cv_df['params'] == 'Test set']['RMSE'], 'o', label='Test set')
plt.xticks(rotation=90)
plt.xlabel('Parameter combinations')
plt.ylabel('RMSE')
plt.legend()
plt.show()
```
或者,您可以使用以下代码将 RMSE 值绘制成柱状图:
```
plt.bar(cv_df['params'], cv_df['RMSE'])
plt.xticks(rotation=90)
plt.xlabel('Parameter combinations')
plt.ylabel('RMSE')
plt.show()
```
希望这些代码可以帮助您实现绘图。
阅读全文