随机森林模型

R 中的降维

Matt Pickard

Owner, Pickard Predictives, LLC

随机森林

  • 集成模型
    • "群体智慧"方法
  • 汇聚多棵随机树的预测
  • 随机、低相关的树可降低误差
  • 避免过拟合
  • 准确
  • 可进行特征选择

一个图示展示由多棵决策树组成的集成模型,以及它们的投票如何合并为最终结果。

R 中的降维

随机森林

该图示展示如何用不同的特征子集创建不同的子树。

R 中的降维

训练随机森林

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 中的降维

¡Vamos a practicar!

R 中的降维

Preparing Video For Download...