隨機森林:預測
現在你需要用隨機森林模型進行預測。語法和梯度提升樹模型相同。
本練習屬於課程
使用 R 的 sparklyr:Spark 入門
練習說明
已為你建立名為 spark_conn 的 Spark 連線。連結到 Spark 中訓練與測試資料集的 tibbles 已預先定義為 track_data_to_model_tbl 與 track_data_to_predict_tbl。隨機森林模型已預先定義為 random_forest_model。
- 定義變數
predicted,其中包含模型對測試資料的預測結果。- 以模型與測試資料作為引數呼叫
ml_predict()。此函式會為測試資料集產生預測,並將其新增為名為prediction的新欄位。
- 以模型與測試資料作為引數呼叫
- 定義變數
responses,以便準備比較預測應變數與實際應變數的資料:- 選取應變數欄位
year。 - 收集結果。
- 使用
mutate()將predicted中的預測加入。
- 選取應變數欄位
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Training, testing sets & model are pre-defined
track_data_to_model_tbl
track_data_to_predict_tbl
random_forest_model
# Predict the responses for the testing data
predicted <- ml_predict(
___,
___) %>% pull(prediction)
# Create a response vs. actual dataset
responses <- ___