可视化模型性能

在 R 中使用 tidymodels 建模

David Svancer

Data Scientist

绘制混淆矩阵

使用 autoplot() 绘制热图

  • 将混淆矩阵对象传入 autoplot()
  • type设为'heatmap'
  • 可视化最常见的计数

 

conf_mat(leads_results,
         truth = purchased,
         estimate = .pred_class) %>%

autoplot(type = 'heatmap')

混淆矩阵热图

在 R 中使用 tidymodels 建模

马赛克图

使用 autoplot() 绘制马赛克图

  • type设为'mosaic'
  • 每个竖条表示该列实际结果值的 100%
  • 直观展示
    • 敏感度

 

conf_mat(leads_results,
         truth = purchased,
         estimate = .pred_class) %>% 
  autoplot(type = 'mosaic')

混淆矩阵马赛克图(敏感度条)

在 R 中使用 tidymodels 建模

马赛克图

使用 autoplot() 绘制马赛克图

  • type设为'mosaic'
  • 每个竖条表示该列实际结果值的 100%
  • 直观展示
    • 敏感度
    • 特异度
conf_mat(leads_results,
         truth = purchased,
         estimate = .pred_class) %>% 
  autoplot(type = 'mosaic')

混淆矩阵马赛克图(特异度条)

在 R 中使用 tidymodels 建模

概率阈值

二分类的默认概率阈值为 0.5

  • 若正类的估计概率≥0.5,则预测为正类

 

leads_results

  • .pred_yes≥0.5,则tidymodelspredict().pred_class设为'yes'
leads_results
# A tibble: 332 x 4
   purchased .pred_class .pred_yes .pred_no
   <fct>     <fct>           <dbl>    <dbl>
 1 no        no             0.134     0.866
 2 yes       yes            0.729     0.271
 3 no        no             0.133     0.867
 4 no        no             0.0916    0.908
 5 yes       yes            0.598     0.402
 6 no        no             0.128     0.872
 7 yes       no             0.112     0.888
 8 no        no             0.169     0.831
 9 no        no             0.158     0.842
10 yes       yes            0.520     0.480
# ... with 322 more rows
在 R 中使用 tidymodels 建模

探索不同阈值下的性能

分类模型在一系列阈值上的表现如何?

  • 测试集结果中.pred_yes列的唯一概率阈值
    • 为每个阈值计算特异度与敏感度

 

threshold specificity sensitivity
0 0 1
0.11 0.01 0.98
0.15 0.05 0.97
... ... ...
0.84 0.89 0.08
0.87 0.94 0.02
0.91 0.99 0
1 1 0
在 R 中使用 tidymodels 建模

可视化不同阈值下的性能

受试者工作特征(ROC)曲线

  • 用于可视化不同概率阈值下的性能

 

  • 测试集各唯一阈值的敏感度 vs.(1 - 特异度)

敏感度对比 1 减去特异度

在 R 中使用 tidymodels 建模

可视化不同阈值下的性能

受试者工作特征(ROC)曲线

  • 用于可视化不同概率阈值下的性能

 

  • 测试集各唯一阈值的敏感度 vs(1 - 特异度)
    • 实际正类中判对的比例 vs 实际负类中判错的比例(加粗为错误)

ROC 曲线

在 R 中使用 tidymodels 建模

ROC 曲线

"最佳性能"位于点 (0, 1)

  • 理想情况下,模型在各阈值上的点应靠近左上边缘

理想的 ROC 曲线

在 R 中使用 tidymodels 建模

ROC 曲线

"最佳性能"位于点 (0, 1)

  • 理想情况下,模型在各阈值上的点应靠近左上边缘

 

"较差性能"

  • 各阈值下敏感度与(1 - 特异度)相等
    • 相当于掷公平硬币随机预测的分类模型

性能较差的 ROC 曲线

在 R 中使用 tidymodels 建模

汇总 ROC 曲线

ROC 曲线下面积(ROC AUC)将分类模型的 ROC 信息汇总为单一数值

可将其直观地解读为"绩点"

  • A - [0.9, 1]
  • B - [0.8, 0.9)
  • C - [0.7, 0.8)
  • D - [0.6, 0.7)
  • F - [0.5, 0.6)

ROC 曲线下面积

在 R 中使用 tidymodels 建模

计算各阈值下的性能

roc_curve() 函数

  • 首个参数为结果 tibble
  • truth 列包含真实类别
  • 含正类估计概率的列
    • leads_results 中为 .pred_yes

 

  • 返回含.pred_yes所有唯一阈值的特异度与敏感度的 tibble
leads_results %>% 
  roc_curve(truth = purchased, .pred_yes)
# A tibble: 331 x 3
   .threshold specificity sensitivity
        <dbl>       <dbl>       <dbl>
 1     -Inf       0             1    
 2     0.0871     0             1    
 3     0.0888     0.00472       1    
 4     0.0893     0.00943       1    
 5     0.0896     0.0142        1    
 6     0.0902     0.0142        0.992
 7     0.0916     0.0142        0.983
 8     0.0944     0.0189        0.983
# ... with 323 more rows
在 R 中使用 tidymodels 建模

绘制 ROC 曲线

roc_curve() 的结果传入 autoplot() 可绘制 ROC 曲线图

 

leads_results %>% 
  roc_curve(truth = purchased, .pred_yes) %>% 
  autoplot()

在 R 中使用 tidymodels 建模

计算 ROC AUC

yardstick 中的 roc_auc() 计算 ROC AUC

  • 模型结果的 tibble
  • truth
  • 正类估计概率所在列
roc_auc(leads_results,
        truth = purchased,
        .pred_yes)
# A tibble: 1 x 3
  .metric  .estimator .estimate
  <chr>      <chr>       <dbl>
1 roc_auc    binary      0.763
在 R 中使用 tidymodels 建模

Vamos praticar!

在 R 中使用 tidymodels 建模

Preparing Video For Download...