使用特徵重要度來縮減資料
現在你已經建立了完整的隨機森林模型,接下來要來看特徵重要度。
雖然隨機森林模型本身就會(但屬於隱性地)進行特徵選擇,建立一個精簡模型往往更有利。精簡模型訓練更快、預測更快,也更容易理解與維護。當然,模型的單純度與效能之間總是需要權衡。
在這個練習中,你會先縮減資料集。下一個練習中,你會擬合一個精簡模型,並把它的效能與完整模型比較。已為你提供 rf_fit、train 與 test。
已為你載入 tidyverse、tidymodels 與 vip 套件。
本練習屬於課程
R 的降維
練習說明
- 使用
vi()並搭配rank參數,擷取最重要的 10 個特徵。 - 把目標變數加回前述的頂尖特徵清單。
- 將頂尖特徵遮罩套用到資料集,以縮減資料。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Extract the top ten features
top_features <- ___ %>%
___(___ = ___) %>%
filter(___) %>%
pull(Variable)
# Add the target variable to the feature list
top_features <- c(___, "___")
# Reduce and print the data sets
train_reduced <- train[___]
test_reduced <- ___[___]
train_reduced %>% head(5)
test_reduced %>% head(5)