建立完整的隨機森林模型
隨機森林模型在建構許多由特徵子集合組成的子樹時,會自然地進行特徵選擇。理解特徵重要度的一種方式,是先建立模型再擷取特徵重要度。因此,在本練習中,你會使用 Healthcare Job Attrition 資料來訓練一個 rand_forest() 的分類模型,接著從中擷取特徵重要度。為了能夠取得特徵重要度,請務必在建立模型時設定 importance = "impurity"。train 與 test 資料集已經為你準備好。
已為你載入 tidyverse、tidymodels 和 vip 套件。
本練習屬於課程
R 的降維
練習說明
- 定義一個具有 200 棵樹的隨機森林分類模型,並能用來擷取特徵重要度。
- 使用所有自變數來配適此隨機森林模型。
- 將預測結果綁定回測試集。
- 計算 F1 指標。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Specify the random forest model
rf_spec <- ___(mode = "___", ___ = ___) %>%
set_engine("___", ___ = "___")
# Fit the random forest model with all predictors
rf_fit <- ___ %>%
___(___, data = ___)
# Create the test set prediction data frame
predict_df <- ___ %>%
bind_cols(predict = ___(___, ___))
# Calculate F1 performance
f_meas(predict_df, ___, .pred_class)