評估訓練誤差
你現在要評估先前練習中建立的迴歸樹 dt 在訓練集上達成的 RMSE。
除了 dt,X_train 與 y_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))