评估模型结果
在得到线性拟合和预测结果后,我们希望评估预测效果,从而判断模型是否可靠。理想情况下,我们会对任何交易策略做回测。不过,这通常较为复杂且耗时。
更快捷的方式是查看回归评估指标(如 R$^2$),并绘制预测值与目标实际值的对比图。若预测完美,在该图上会形成一条笔直的对角线。这样我们就能直观观察不同涨跌幅区间里的预测表现。我们可以使用 matplotlib 的 .scatter() 函数绘制预测值与实际值的散点图。
本练习是课程的一部分
Python 金融机器学习
练习说明
- 使用散点图显示
test_predictions与test_targets的关系,点的不透明度设为 20%(用alpha参数设置不透明度)。 - 使用
np.arange()以及 x 轴的最小值和最大值(xmin、xmax)绘制理想预测的对角线。 - 使用
plt.legend()在图中显示图例。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Scatter the predictions vs the targets with 20% opacity
plt.scatter(train_predictions, train_targets, alpha=0.2, color='b', label='train')
plt.scatter(____, ____, ____, color='r', label='test')
# Plot the perfect prediction line
xmin, xmax = plt.xlim()
plt.plot(np.arange(xmin, xmax, 0.01), np.arange(____, ____, 0.01), c='k')
# Set the axis labels and show the plot
plt.xlabel('predictions')
plt.ylabel('actual')
____ # show the legend
plt.show()