在測試集上進行預測
在上一個練習中,線性迴歸與 ridge 看起來表現相近。你可以選擇其中任一模型;不過,還是可以在測試集上檢查預測效能,看看是否有模型能勝過另一個。
你將使用均方根誤差(RMSE)作為評估指標。已為你預先載入包含兩個模型名稱與實例的字典 models,以及訓練與目標陣列 X_train_scaled、X_test_scaled、y_train 和 y_test。
本練習屬於課程
使用 scikit-learn 進行監督式學習
練習說明
- 匯入
root_mean_squared_error。 - 將模型擬合到已縮放的訓練特徵與訓練標籤。
- 使用已縮放的測試特徵進行預測。
- 將測試集標籤與預測標籤傳入以計算 RMSE。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import root_mean_squared_error
from ____.____ import ____
for name, model in models.items():
# Fit the model to the training data
____
# Make predictions on the test set
y_pred = ____
# Calculate the test_rmse
test_rmse = ____(____, ____)
print("{} Test Set RMSE: {}".format(name, test_rmse))