視覺化模型表現

在 R 中使用 tidymodels 建立模型

David Svancer

Data Scientist

繪製混淆矩陣

使用 autoplot() 繪製熱圖

  • 將混淆矩陣物件傳入 autoplot()
  • type 設為 'heatmap'
  • 視覺化最常見的計數

 

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

autoplot(type = 'heatmap')

混淆矩陣熱圖

在 R 中使用 tidymodels 建立模型

鑲嵌圖(Mosaic)

使用 autoplot() 繪製鑲嵌圖(mosaic)

  • type 設為 'mosaic'
  • 每個直條代表該欄實際結果的 100%
  • 可視化呈現
    • sensitivity(靈敏度)

 

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

混淆矩陣鑲嵌圖靈敏度長條

在 R 中使用 tidymodels 建立模型

鑲嵌圖(Mosaic)

使用 autoplot() 繪製鑲嵌圖(mosaic)

  • type 設為 'mosaic'
  • 每個直條代表該欄實際結果的 100%
  • 可視化呈現
    • sensitivity(靈敏度)
    • specificity(特異度)
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 欄的唯一機率門檻
    • 逐一計算 specificity 與 sensitivity

 

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(接收者操作特徵)曲線

  • 用來視覺化不同機率門檻下的表現

 

  • 測試集各門檻下 sensitivity 對 (1 - specificity) 的關係

靈敏度對 1 減特異度

在 R 中使用 tidymodels 建立模型

視覺化跨門檻的表現

ROC(接收者操作特徵)曲線

  • 用來視覺化不同機率門檻下的表現

 

  • 測試集各門檻下的 sensitivity 對 (1 - specificity)
    • 實際正類中的正確比例 vs. 實際負類中的「錯誤」比例

ROC 曲線

在 R 中使用 tidymodels 建立模型

ROC 曲線

最佳表現 位於點 (0, 1)

  • 理想情況下,分類模型在各門檻下的點應靠近左上邊緣

理想 ROC 曲線

在 R 中使用 tidymodels 建立模型

ROC 曲線

最佳表現 位於點 (0, 1)

  • 理想情況下,分類模型在各門檻下的點應靠近左上邊緣

 

較差表現

  • 在所有門檻下,sensitivity 與 (1 - specificity) 相等
    • 等同於用公平擲硬幣的結果來預測

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 tibble 中為 .pred_yes

 

  • 回傳含 .pred_yes 各唯一門檻之 specificity 與 sensitivity 的 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

yardstickroc_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 建立模型

一起來練習吧!

在 R 中使用 tidymodels 建立模型

Preparing Video For Download...