更多模型衡量

R 的樹狀模型機器學習

Sandro Raabe

Data Scientist

準確率的侷限

 

  • 「天真」模型總是預測 no,也可能有 98% 的準確率

$\rightarrow$ 當資料集極度不平衡且 98% 為負樣本時可能發生

R 的樹狀模型機器學習

Sensitivity(靈敏度、真陽性率)

  • 所有正類中被正確分類的比例

confusion matrix showing sensitivity

R 的樹狀模型機器學習

Specificity(特異度、真陰性率)

  • 所有負類中被正確分類的比例

confusion matrix showing true negative rate

R 的樹狀模型機器學習

table showing different thresholds

R 的樹狀模型機器學習

ROC(Receiver Operating Characteristic)曲線

  • 視覺化分類模型在所有可能閾值下的表現

ROC curve plot

R 的樹狀模型機器學習

ROC 曲線與 AUC

different ROC curves

R 的樹狀模型機器學習

ROC 曲線下面積(AUC)

AUC plot

  • AUC = 0.5
  • 表現不優於隨機

 

  • AUC = 1
  • 在每個閾值都分類正確 $\rightarrow$ 完美模型

 

  • AUC = 0
  • 每個樣本都被錯誤分類
R 的樹狀模型機器學習

yardstick:sens()(靈敏度)

predictions
# A tibble: 153 x 2
.pred_class   true_class
      <fct>        <fct>     
 1      yes           no        
 2       no           no        
 3       no          yes       
 4      yes          yes
# Calculate single-threshold sensitivity
sens(predictions, 
     estimate = .pred_class, 
     truth = true_class)
# A tibble: 1 x 2
  .metric        .estimate
  <chr>              <dbl>
1 sensitivity        0.872
  • 參數與 accuracy()conf_mat() 類似
R 的樹狀模型機器學習

yardstick:roc_curve()(ROC)

# Predict probabilities on test set
predictions <- predict(model,
                       data_test,

type = "prob") %>%
bind_cols(data_test)
# A tibble: 9,116 x 13
   .pred_yes still_customer age gender    ...
       <dbl> <fct>         <int> <fct>    ...
 1    0.0557 no               45 M        ...
 2    0.0625 no               49 F        ...
 3    0.330  no               51 M        ...
 4    ...
 ...
# Calculate the ROC curve for all thresholds
roc <- roc_curve(predictions,

estimate = .pred_yes,
truth = still_customer)
# Plot the ROC curve autoplot(roc)

roc curve

R 的樹狀模型機器學習

yardstick:roc_auc()(AUC)

  • 相同參數:資料、預測欄、真實欄
# Calculate area under curve
roc_auc(predictions, 
        estimate = .pred_yes, 
        truth = still_customer)
# A tibble: 1 x 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 roc_auc binary         0.872
R 的樹狀模型機器學習

一起來測量吧!

R 的樹狀模型機器學習

Preparing Video For Download...