開始使用免費開始

評估結果

當我們得到線性擬合和預測之後,就要檢視預測表現,以判斷模型是否可用。理想情況下,我們會對任何交易策略進行回測。不過,這通常很複雜且耗時。

更快了解模型表現的方法,是查看像 R\(^2\) 這樣的迴歸評估指標,並將預測值與目標的實際值繪成對照圖。在這種圖上,完美的預測會形成一條筆直的對角線,讓我們能用肉眼判斷在不同價格變動區間的預測表現。我們可以使用 matplotlib.scatter() 函式來建立預測值與實際值的散佈圖。

本練習屬於課程

Python 金融 Machine Learning

檢視課程

練習說明

  • 以散佈圖顯示 test_predictionstest_targets,點的透明度設為 20%(使用 alpha 參數設定不透明度)。
  • 使用 np.arange() 搭配 x 軸的最小與最大值(xminxmax)繪出完美預測線。
  • 使用 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()
編輯並執行程式碼