評估模型擬合度

在 R 中使用 tidymodels 建立模型

David Svancer

Data Scientist

二元分類

兩水準的輸出變數

  • 正類
    • 你要預測的事件
    • purchased 變數中的「yes」
  • 負類

    • 「no」
  • tidymodels 中,輸出變數需為因子

    • 第一個層級是正類
    • levels() 檢查順序
leads_df
# A tibble: 1,328 x 7
  purchased total_visits  ...   us_location
   <fct>        <dbl>     ...     <fct>
 1 yes            7       ...     west
 2 no             8       ...     west
 3 no             5       ...     southeast
# ... with 1,325 more rows
levels(leads_df[['purchased']])
[1] "yes" "no"
在 R 中使用 tidymodels 建立模型

混淆矩陣

 

實際與預測結果各種組合的計數矩陣

正確預測

  • 真陽性(TP)
  • 真陰性(TN)

分類錯誤

  • 假陽性(FP)
  • 假陰性(FN)

 

混淆矩陣

在 R 中使用 tidymodels 建立模型

yardstick 的分類指標

使用 yardstick 建立混淆矩陣與其他擬合指標

  • 需要包含以下欄位的模型結果 tibble:
    • 真實輸出值
      • purchased
    • 預測的類別
      • .pred_class
    • 各類別的機率估計
      • .pred_yes
      • .pred_no
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 建立模型

用 yardstick 建立混淆矩陣

conf_mat() 函式

  • 模型結果的 tibble
  • truth:真實結果的欄位
  • estimate:預測結果的欄位

leads_df 的羅吉斯迴歸

  • 正確分類 332 位顧客中的 252 位(76%)
  • 46 個假陰性
  • 34 個假陽性
conf_mat(leads_results,

truth = purchased,
estimate = .pred_class)
          Truth
Prediction yes  no
       yes  74  34
       no   46 178
在 R 中使用 tidymodels 建立模型

分類正確率

accuracy() 函式

  • 參數與 conf_mat() 相同
  • 計算分類正確率

 

$$\frac{TP + TN}{TP + TN + FP + FN}$$

 

  • yardstick 函式皆回傳 tibble
    • .metric:指標種類
    • .estimate:計算值
accuracy(leads_results, 
         truth = purchased, 
         estimate = .pred_class)
# A tibble: 1 x 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.759
在 R 中使用 tidymodels 建立模型

靈敏度

許多情況下,accuracy 不是最佳指標

  • leads_df 資料
    • 全部預測為「no」也有 64% 正確率

 

靈敏度(Sensitivity)

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

  • 對於「有購買」的顧客,模型正確預測的比例是多少?
    • 假陰性越少,靈敏度越高

靈敏度計算

在 R 中使用 tidymodels 建立模型

計算靈敏度

sens() 函式

  • 參數與 conf_mat()accuracy() 相同
  • .estimate 欄回傳靈敏度
sens(leads_results, 
     truth = purchased, 
     estimate = .pred_class)
# A tibble: 1 x 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 sens    binary         0.617
在 R 中使用 tidymodels 建立模型

特異度

特異度(Specificity) 是所有負類中被正確分類的比例

  • 對於「沒有購買」的顧客,模型正確預測的比例是多少?
    • 假陽性越少,特異度越高

 

1 - 特異度

  • 也稱為「假陽性率」(FPR)
  • 在真陰性中,假陽性的比例

特異度計算

在 R 中使用 tidymodels 建立模型

計算特異度

spec() 函式

  • 參數與 sens() 相同
  • .estimate 欄回傳特異度
spec(leads_results, 
     truth = purchased, 
     estimate = .pred_class)
# A tibble: 1 x 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 spec    binary         0.840
在 R 中使用 tidymodels 建立模型

建立指標集合

自訂指標集合

  • metric_set() 函式
    • 以選定的 yardstick 指標建立自訂指標函式
    • yardstick 指標函式名稱傳入 metric_set()
    • 用自訂函式計算指標
custom_metrics <-
  metric_set(accuracy, sens, spec)
custom_metrics(leads_results, 
               truth = purchased, 
               estimate = .pred_class)
# A tibble: 3 x 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.759
2 sens     binary         0.617
3 spec     binary         0.840
在 R 中使用 tidymodels 建立模型

多種指標

二元分類指標

  • 指標種類很多

    • accuracy(), kap(), sens(), spec(), ppv(), npv(), mcc(), j_index(), bal_accuracy(), detection_prevalence(), precision(), recall(), f_meas()
  • conf_mat() 的結果傳給 summary() 可一次計算

 

https://yardstick.tidymodels.org/reference

conf_mat(leads_results, truth = purchased, 
         estimate = .pred_class) %>% 
  summary()
# A tibble: 13 x 3
   .metric              .estimator .estimate
   <chr>                <chr>          <dbl>
 1 accuracy             binary         0.759
 2 kap                  binary         0.466
 3 sens                 binary         0.617
 4 spec                 binary         0.840
 5 ppv                  binary         0.685
 6 npv                  binary         0.795
 7 mcc                  binary         0.468
 8 j_index              binary         0.456
 9 bal_accuracy         binary         0.728
10 detection_prevalence binary         0.325
11 precision            binary         0.685
12 recall               binary         0.617
13 f_meas               binary         0.649
在 R 中使用 tidymodels 建立模型

一起來練習吧!

在 R 中使用 tidymodels 建立模型

Preparing Video For Download...