# 计算四种方法的测试集MSE
mse_values = [ # MSE列表
mean_squared_error(y_test, single_tree.predict(X_test)), # 单棵树MSE
mean_squared_error(y_test, bagging.predict(X_test)), # Bagging MSE
mean_squared_error(y_test, rf.predict(X_test)), # 随机森林MSE
mean_squared_error(y_test, boosting.predict(X_test)) # Boosting MSE
]
methods = ['单棵树', 'Bagging', '随机森林', 'Boosting'] # 方法标签
plt.figure(figsize=(10, 6)) # 设置画布
bars = plt.bar(methods, mse_values, color=['#E3120B', '#2C3E50', '#008080', '#F0A700']) # 柱状图
for bar, val in zip(bars, mse_values): # 添加数值标签
plt.text(bar.get_x() + bar.get_width()/2., bar.get_height(), f'{val:.4f}',
ha='center', va='bottom', fontsize=11) # 柱顶标注MSE值
plt.title('ROE预测: 不同树方法的测试集MSE比较', fontsize=14) # 图表标题
plt.ylabel('均方误差 (MSE)') # Y轴标签
plt.grid(axis='y', alpha=0.3) # Y轴网格线
plt.tight_layout() # 自动布局
plt.show() # 显示图表