評估效能
最後,一如往常,你需要評估最佳模型的效能,看看表現好或不好。理想情況下會做回溯測試(back-testing),但這是個較完整的流程,本課程沒有篇幅涵蓋。
我們已經看過 R\(^2\) 分數了,現在改用 matplotlib 繪製預測值對實際值的散佈圖。若是完美預測,點會落在從左下到右上的對角線上。
本練習屬於課程
Python 金融 Machine Learning
練習說明
- 在
RandomForestRegressor(rfr)中使用上一個練習找到的最佳max_features數值(為 4)。 - 使用模型對
train_features與test_features進行預測。 - 繪製實際目標(train/test_targets)與預測值(train/test_predictions)的散佈圖,並將資料集標記為
train與test。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Use the best hyperparameters from before to fit a random forest model
rfr = RandomForestRegressor(n_estimators=200, max_depth=3, max_features=____, random_state=42)
rfr.fit(train_features, train_targets)
# Make predictions with our model
train_predictions = rfr.predict(____)
test_predictions = ____
# Create a scatter plot with train and test actual vs predictions
plt.scatter(train_targets, train_predictions, label='train')
plt.scatter(____)
plt.legend()
plt.show()