開始使用免費開始

檢查結果

當我們有了最佳化後的模型,就該更進一步檢查它的表現。我們已經看過 R\(^2\) 分數,但把預測值與實際值畫在圖上常常更有幫助。我們可以用決策樹模型的 .predict() 方法,對訓練集和測試集進行預測。

理想情況下,你會看到從左下到右上的對角線。然而,由於決策樹本身較為簡單,我們的模型在測試集上表現不會太好。不過在訓練集上會表現不錯。

本練習屬於課程

Python 金融 Machine Learning

檢視課程

練習說明

  • 建立一個 DecisionTreeRegressor 模型,命名為 decision_tree,並將 max_depth 超參數設為 3。
  • 使用我們的決策樹模型,對訓練集與測試集(train_featurestest_features)進行預測。
  • 使用 plt.scatter() 將訓練與測試的預測值分別與實際目標值作散佈圖,並在測試集的圖上將 label 參數設為 test

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Use the best max_depth of 3 from last exercise to fit a decision tree
decision_tree = ____
decision_tree.fit(train_features, train_targets)

# Predict values for train and test
train_predictions = decision_tree.predict(train_features)
test_predictions = ____

# Scatter the predictions vs actual values
plt.scatter(train_predictions, train_targets, label='train')
plt.scatter(____)
plt.show()
編輯並執行程式碼