進行預測
用資料做預測是機器學習的基礎目標之一。 現在你已經知道如何切分資料並擬合模型,是時候用你的模型對看不見的樣本進行預測了。
你將使用以訓練資料配適到樹狀規格所得到的模型,來對測試集進行預測。
你的工作環境中已提供先前產生的資料集(diabetes_train 與 diabetes_test)以及決策樹規格 tree_spec,它是用以下程式碼建立的:
tree_spec <- decision_tree() %>%
set_engine("rpart") %>%
set_mode("classification")
本練習屬於課程
R 的樹狀模型機器學習
練習說明
- 使用
outcome作為目標變數,並納入所有預測變數,將你的規格配適到訓練資料以建立model。 - 使用你的模型對測試集中每筆觀測的糖尿病結果進行預測,並將結果指定給
predictions。 - 將測試集的真實結果加入到
predictions,欄位名稱為true_class,並將結果儲存為predictions_combined。 - 使用
head()函式列印結果的前幾列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Train your model
model <- tree_spec %>%
___
# Generate predictions
predictions <- ___(model,
___)
# Add the true outcomes
predictions_combined <- predictions %>%
___(true_class = ___)
# Print the first lines of the result
___