用交叉驗證評估效能

在 R 中使用 tidymodels 建立模型

David Svancer

Data Scientist

訓練與測試資料集

 

建立訓練與測試資料集是建模的第一步

  • 可防止過度擬合
    • 訓練資料用於配適模型
    • 測試資料用於評估模型

 

缺點

  • 只有一個模型效能估計值

 

建立訓練與測試資料集的分割示意圖

在 R 中使用 tidymodels 建立模型

K 折交叉驗證

用於探索模型效能的重抽樣技巧

  • 在配適過程中提供 K 個模型效能估計

 

將資料分為訓練與測試集

在 R 中使用 tidymodels 建立模型

K 折交叉驗證

用於探索模型效能的重抽樣技巧

  • 在配適過程中提供 K 個模型效能估計
  • 將訓練資料隨機分成 K 個大小相近的集合
  • 使用這些摺疊進行 K 次配適與評估

 

將訓練資料分成交叉驗證摺疊

在 R 中使用 tidymodels 建立模型

用交叉驗證進行機器學習

執行 5 折交叉驗證

  • 進行 5 次模型訓練與評估

 

五折交叉驗證的第 1 次迭代

在 R 中使用 tidymodels 建立模型

用交叉驗證進行機器學習

執行 5 折交叉驗證

  • 進行 5 次模型訓練與評估
  • 第 1 次迭代
    • 摺疊 1 保留做模型評估,摺疊 2 到 5 用於模型訓練

 

五折交叉驗證的第 1 次迭代

在 R 中使用 tidymodels 建立模型

用交叉驗證進行機器學習

執行 5 折交叉驗證

  • 進行 5 次模型訓練與評估
  • 第 1 次迭代
    • 摺疊 1 保留做模型評估,摺疊 2 到 5 用於模型訓練
  • 第 2 次迭代
    • 摺疊 2 保留做模型評估

 

五折交叉驗證的第 2 次迭代

在 R 中使用 tidymodels 建立模型

用交叉驗證進行機器學習

執行 5 折交叉驗證

  • 進行 5 次模型訓練與評估
  • 第 1 次迭代
    • 摺疊 1 保留做模型評估,摺疊 2 到 5 用於模型訓練
  • 第 2 次迭代
    • 摺疊 2 保留做模型評估

 

總共有 五個模型效能估計值

 

五折交叉驗證的第 5 次迭代

在 R 中使用 tidymodels 建立模型

建立交叉驗證摺疊

vfold_cv() 函式

  • 訓練資料
  • 摺疊數,v
  • 分層變數,strata
  • 為了可重現性,在 vfold_cv() 前先執行 set.seed()
  • splits
    • 清單欄位,包含建立摺疊的資料分割物件
set.seed(214)
leads_folds <- vfold_cv(leads_training,

v = 10,
strata = purchased)
leads_folds
#  10-fold cross-validation using stratification 
# A tibble: 10 x 2
   splits            id    
   <list>            <chr> 
 1 <split [896/100]> Fold01
 2 <split [896/100]> Fold02
 3 <split [896/100]> Fold03
 . ................  ......
 9 <split [897/99]>  Fold09
10 <split [897/99]>  Fold10
在 R 中使用 tidymodels 建立模型

用交叉驗證進行模型訓練

fit_resamples() 函式

  • 訓練一個 parsnip 模型或 workflow 物件
  • 提供交叉驗證摺疊,resamples
  • 可選的自訂衡量函式,metrics
    • 預設為 accuracy 與 ROC AUC

 

每個指標會估計 10 次

  • 每個摺疊一個估計值
  • mean 欄為平均值
leads_rs_fit <- leads_wkfl %>%

fit_resamples(resamples = leads_folds,
metrics = leads_metrics)
leads_rs_fit %>% collect_metrics()
# A tibble: 3 x 5
  .metric .estimator  mean     n std_err
  <chr>   <chr>      <dbl> <int>   <dbl>
1 roc_auc binary     0.823    10  0.0147
2 sens    binary     0.786    10  0.0203
3 spec    binary     0.855    10  0.0159
在 R 中使用 tidymodels 建立模型

交叉驗證結果詳情

collect_metrics() 函式

  • 傳入 summarize = FALSE 可取得每個交叉驗證摺疊的所有指標估計
  • 共 30 組組合(3 個指標 x 10 個摺疊)
    • .metric 欄識別指標
    • .estimate 欄為各摺疊的估計值
rs_metrics <- leads_rs_fit %>% 
  collect_metrics(summarize = FALSE)

rs_metrics
# A tibble: 30 x 4
   id     .metric .estimator .estimate
   <chr>  <chr>   <chr>          <dbl>
 1 Fold01 sens    binary         0.861
 2 Fold01 spec    binary         0.891
 3 Fold01 roc_auc binary         0.885
 4 Fold02 sens    binary         0.778
 5 Fold02 spec    binary         0.969
 6 Fold02 roc_auc binary         0.885
# ... with 24 more rows
在 R 中使用 tidymodels 建立模型

彙整交叉驗證結果

collect_metrics() 會回傳一個 tibble

  • 可用 dplyr 彙整結果
    • rs_metrics 起始
    • .metric 分組
    • summarize() 計算統計量
rs_metrics %>%

group_by(.metric) %>%
summarize(min = min(.estimate), median = median(.estimate), max = max(.estimate), mean = mean(.estimate), sd = sd(.estimate))
# A tibble: 3 x 6
 .metric   min  median   max   mean     sd
  <chr>   <dbl>  <dbl>  <dbl>  <dbl>   <dbl>
1 roc_auc 0.758  0.806  0.885  0.823   0.0466
2 sens    0.667  0.792  0.861  0.786   0.0642
3 spec    0.810  0.843  0.969  0.855   0.0502
在 R 中使用 tidymodels 建立模型

交叉驗證方法論

fit_resamples() 訓練的模型無法對新資料來源產生預測

  • predict() 不接受重抽樣物件

fit_resample() 的目的

  • 探索並比較不同模型類型的效能表現
  • 選出表現最佳的模型類型,再進一步專注於配適
predict(leads_rs_fit,
        new_data = leads_test)

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to 
  an object of class 
  "c('resample_results', 
      'tune_results',  
      'tbl_df', 
      'tbl', 'data.frame')"
在 R 中使用 tidymodels 建立模型

一起來練習吧!

在 R 中使用 tidymodels 建立模型

Preparing Video For Download...