開始使用免費開始

評估訓練誤差

你現在要評估先前練習中建立的迴歸樹 dt 在訓練集上達成的 RMSE。

除了 dtX_trainy_train 也已經在你的工作區可用。

請注意,在 scikit-learn 中,可以這樣計算模型的 MSE:

MSE_model = mean_squared_error(y_true, y_predicted)

其中我們使用 metrics 模組裡的 mean_squared_error 函式,第一個引數是實際標籤 y_true,第二個引數是模型的預測標籤 y_predicted

本練習屬於課程

Machine Learning with Tree-Based Models in Python

檢視課程

練習說明

  • sklearn.metrics 匯入 mean_squared_error 並命名為 MSE
  • dt 擬合到訓練集。
  • 預測 dt 的訓練集標籤,並將結果指定給 y_pred_train
  • 評估 dt 的訓練集 RMSE,並將它指定給 RMSE_train

動手互動練習

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

# Import mean_squared_error from sklearn.metrics as MSE
____

# Fit dt to the training set
____.____(____, ____)

# Predict the labels of the training set
____ = ____.____(____)

# Evaluate the training set RMSE of dt
____ = (____(____, ____))**(___)

# Print RMSE_train
print('Train RMSE: {:.2f}'.format(RMSE_train))
編輯並執行程式碼