隨機森林模型

R 的降維

Matt Pickard

Owner, Pickard Predictives, LLC

Random Forest

  • 集成模型
    • 「群眾智慧」方法
  • 彙整多棵隨機樹的預測
  • 隨機且低相關的樹可降低誤差
  • 避免過度擬合
  • 準確
  • 會做特徵選擇

一個圖示,顯示由多棵決策樹組成的集成模型,以及如何將它們的投票彙總成最終結果。

R 的降維

Random Forest

此圖示說明如何用不同的特徵子集建立不同的子樹。

R 的降維

訓練 Random Forest

library(tidymodels)

rf <- rand_forest(mode = "classification", trees = 200) %>% set_engine("ranger", importance = "impurity")
rf_fit <- rf %>% fit(credit_score ~ ., data = train)
predict_df <- test %>% bind_cols(predict = predict(rf_fit, test))
R 的降維

評估模型

f_meas(predict_df, credit_score, .pred_class)
0.6895
R 的降維

變數重要性

library(vip)

rf_fit %>% vip()

變數重要性長條圖。

R 的降維

特徵遮罩

top_features <- rf_fit %>% 
  vi(rank = TRUE) %>% 
  filter(Importance <= 10) %>% 
  pull(Variable)

top_features
 [1] "outstanding_debt"        "interest_rate"          
 [3] "delay_from_due_date"     "changed_credit_limit"   
 [5] "credit_history_months"   "num_credit_card"        
 [7] "monthly_balance"         "num_of_delayed_payment" 
 [9] "annual_income"           "amount_invested_monthly"
R 的降維

縮減資料

train_reduced <- train[top_features]
test_reduced <- test[top_features]
R 的降維

效能

rf_fit <- rf %>% 
  fit(credit_score ~ ., data = train_reduced) 

predict_reduced_df <- test_reduced %>% bind_cols(predict = predict(rf_fit, test_reduced))
f_meas(predict_reduced_df, credit_score, .pred_class)
0.6738 

未縮減模型的 F 分數:

0.6895 
R 的降維

一起來練習吧!

R 的降維

Preparing Video For Download...