模型对比

R 中的树模型机器学习

Sandro Raabe

Data Scientist

动机

比较 AUC
# A tibble: 4 x 3
  model            .metric   .estimate
1 decision_tree    roc_auc       <?>
2 bagged_trees     roc_auc       <?>
3 random_forest    roc_auc       <?>
4 boosted_trees    roc_auc       <?>
比较 ROC 曲线

ROC 曲线对比

R 中的树模型机器学习

合并预测值

bind_cols(decision_tree
                                                   )
# A tibble: 1,011 x 1
   preds_tree
        <dbl> 
 1      0.144
 2      0.441
 3      0.144
 4      0.776
 5      0.441
 6      0.144
 7      0.144
 8      0.441
# ... 另有 1,003 行
R 中的树模型机器学习

合并预测值

bind_cols(decision_tree, bagged_trees
                                                   )
# A tibble: 1,011 x 2
   preds_tree preds_bagging
        <dbl>         <dbl>
 1      0.144         0.115
 2      0.441         0.326
 3      0.144         0.115
 4      0.776         0.773
 5      0.441         0.326
 6      0.144         0.115
 7      0.144         0.115
 8      0.441         0.877
# ... 另有 1,003 行
R 中的树模型机器学习

合并预测值

bind_cols(decision_tree, bagged_trees, random_forest
                                                   )
# A tibble: 1,011 x 3
   preds_tree preds_bagging preds_forest
        <dbl>         <dbl>        <dbl>
 1      0.144         0.115        0    
 2      0.441         0.326        0    
 3      0.144         0.115        0    
 4      0.776         0.773        0.286
 5      0.441         0.326        0.15 
 6      0.144         0.115        0    
 7      0.144         0.115        0    
 8      0.441         0.877        0.7  
# ... 另有 1,003 行
R 中的树模型机器学习

合并预测值

bind_cols(decision_tree, bagged_trees, random_forest, boosted_trees
                                                   )
# A tibble: 1,011 x 4
   preds_tree preds_bagging preds_forest preds_boosting
        <dbl>         <dbl>        <dbl>          <dbl>
 1      0.144         0.115        0              0.136
 2      0.441         0.326        0              0.149
 3      0.144         0.115        0              0.116
 4      0.776         0.773        0.286          0.319
 5      0.441         0.326        0.15           0.199
 6      0.144         0.115        0              0.116
 7      0.144         0.115        0              0.116
 8      0.441         0.877        0.7            0.823
# ... 另有 1,003 行
R 中的树模型机器学习

合并预测值

bind_cols(decision_tree, bagged_trees, random_forest, boosted_trees,
          customers_test %>% select(still_customer))
# A tibble: 1,011 x 5
   preds_tree preds_bagging preds_forest preds_boosting still_customer
        <dbl>         <dbl>        <dbl>          <dbl>          <fct>         
 1      0.144         0.115        0              0.136             no 
 2      0.441         0.326        0              0.149             no
 3      0.144         0.115        0              0.116             no
 4      0.776         0.773        0.286          0.319            yes
 5      0.441         0.326        0.15           0.199             no
 6      0.144         0.115        0              0.116             no
 7      0.144         0.115        0              0.116             no
 8      0.441         0.877        0.7            0.823            yes
# ... 另有 1,003 行
R 中的树模型机器学习

计算决策树 AUC

# 计算 AUC 指标
roc_auc(preds_combined, truth = still_customer, estimate = preds_tree)
# A tibble: 1 x 2
  .metric   .estimate
  <chr>         <dbl>
1 roc_auc       0.911
R 中的树模型机器学习

计算装袋树 AUC

# 计算 AUC 指标
roc_auc(preds_combined, truth = still_customer, estimate = preds_bagging)
# A tibble: 1 x 2
  .metric   .estimate
  <chr>         <dbl>
1 roc_auc       0.936
R 中的树模型机器学习

计算随机森林 AUC

# 计算 AUC 指标
roc_auc(preds_combined, truth = still_customer, estimate = preds_forest)
# A tibble: 1 x 2
  .metric   .estimate
  <chr>         <dbl>
1 roc_auc       0.974
R 中的树模型机器学习

计算提升法 AUC

# 计算 AUC 指标
roc_auc(preds_combined, truth = still_customer, estimate = preds_boosting)
# A tibble: 1 x 2
  .metric   .estimate
  <chr>         <dbl>
1 roc_auc       0.984
R 中的树模型机器学习

合并所有 AUC

# 合并各模型的 AUC
bind_rows(roc_auc(preds_combined, truth = still_customer, estimate = preds_tree), 
          roc_auc(preds_combined, truth = still_customer, estimate = preds_bagging), 
          roc_auc(preds_combined, truth = still_customer, estimate = preds_forest), 
          roc_auc(preds_combined, truth = still_customer, estimate = preds_boosting))

# A tibble: 4 x 2
  .metric .estimate
  <chr>       <dbl>
1 roc_auc     0.911
2 roc_auc     0.936
3 roc_auc     0.974
4 roc_auc     0.984
R 中的树模型机器学习

合并所有 AUC

# 合并各模型的 AUC
bind_rows(decision_tree = roc_auc(preds_combined, truth = still_customer, preds_tree), 
          bagged_trees  = roc_auc(preds_combined, truth = still_customer, preds_bagging), 
          random_forest = roc_auc(preds_combined, truth = still_customer, preds_forest), 
          boosted_trees = roc_auc(preds_combined, truth = still_customer, preds_boosting),
          .id = "model")
# A tibble: 4 x 3
  model            .metric   .estimate
  <chr>            <chr>         <dbl>
1 decision_tree    roc_auc       0.911
2 bagged_trees     roc_auc       0.936
3 random_forest    roc_auc       0.974
4 boosted_trees    roc_auc       0.984
R 中的树模型机器学习

重整结果格式

# 将预测转为长格式
predictions_long <- tidyr::pivot_longer(preds_combined,

cols = starts_with("preds_"),
names_to = "model",
values_to = "predictions")
# A tibble: 4,044 x 3
   still_customer   model          predictions
   <fct>            <chr>                <dbl>
 1 no               preds_tree          0.144 
 2 no               preds_bagging       0.102 
 3 no               preds_forest        0.0333
 4 no               preds_boosting      0.169 
 5 yes              preds_tree          0.441 
 6 no               preds_bagging       0.285 
 7 no               preds_forest        0.36  
 8 no               preds_boosting      0.184 
# ... 另有 4,036 行
R 中的树模型机器学习

计算阈值指标

# 按模型分组
cutoffs <- predictions_long %>% 
      group_by(model) %>%

# 计算每个阈值的指标 roc_curve(truth = still_customer, estimate = predictions)
# A tibble: 668 x 4
# Groups:   model [4]
model         .threshold specificity sensitivity
   <chr>              <dbl>       <dbl>       <dbl>
 1 preds_bagging       -Inf       0           1    
 2 preds_bagging     0.0157       0           1    
 3 preds_bagging     0.0202       0.536       0.975
 4 preds_bagging     0.0254       0.537       0.975
 5 preds_bagging     0.0271       0.665       0.938
 6 preds_bagging     0.0315       0.681       0.938
 # ... 另有 662 行
R 中的树模型机器学习

绘制 ROC 曲线

# 转为可视化
autoplot(cutoffs)

ROC 曲线对比

R 中的树模型机器学习

来比较一下!

R 中的树模型机器学习

Preparing Video For Download...