选择最佳模型

在 R 中使用 tidymodels 建模

David Svancer

Data Scientist

调优结果详解

collect_metrics() 默认提供汇总结果

  • 传入 summarize = FALSE 可返回所有超参数调优结果
dt_tuning %>% 
  collect_metrics(summarize = FALSE)
# A tibble: 150 x 8
 id     cost_complexity tree_depth min_n .metric  ...  .estimate  .config
<chr>        <dbl>         <int>   <int>  <chr>   ...    <dbl>      <chr>  
Fold01    0.0000000758     14       39    sens    ...     0.75     Model1 
Fold01    0.0000000758     14       39    spec    ...     0.906    Model1 
Fold01    0.0000000758     14       39    roc_auc ...     0.888    Model1 
.....     ............     ..       ..    ......  ...     .....    ......
Fold10    0.00380          5        36    roc_auc ...     0.789    Model5
在 R 中使用 tidymodels 建模

探索调优结果

collect_metrics() 中设定 summarise = FALSE 返回一个 tibble

  • 便于用 dplyr 探索结果
  • 探索 ROC AUC
    • 选择 roc_auc 指标
    • id 列分组
    • 计算 .estimate 的统计量
dt_tuning %>% 
  collect_metrics(summarize = FALSE) %>% 

filter(.metric == 'roc_auc') %>%
group_by(id) %>%
summarize(min_roc_auc = min(.estimate), median_roc_auc = median(.estimate), max_roc_auc = max(.estimate))
# A tibble: 10 x 4
 id     min_roc_auc  median_roc_auc  max_roc_auc
<chr>      <dbl>          <dbl>       <dbl>
Fold01     0.830          0.885       0.888
Fold02     0.857          0.882       0.885
Fold03     0.818          0.836       0.836
......     ....           ....        ....
Fold10     0.762          0.790       0.813
在 R 中使用 tidymodels 建模

查看表现最佳的模型

show_best() 函数

  • metric 的平均值显示前 n 个模型
  • Model1 为优胜者
dt_tuning %>% 
  show_best(metric = 'roc_auc', n = 5)
# A tibble: 5 x 9
cost_complexity  tree_depth  min_n  .metric .estimator   mean    n    std_err  .config
    <dbl>           <int>    <int>    <chr>   <chr>      <dbl>  <int>  <dbl>    <chr>
0.0000000758         14       39     roc_auc  binary     0.827   10   0.0147   Model1 
0.00380               5       36     roc_auc  binary     0.825   10   0.0146   Model5 
0.0243                5       34     roc_auc  binary     0.823   10   0.0147   Model2 
0.00000443           11       8      roc_auc  binary     0.816   10   0.00786  Model3 
0.000000600           3       5      roc_auc  binary     0.814   10   0.0131   Model4
在 R 中使用 tidymodels 建模

选择模型

select_best() 函数

  • dt_tuning 结果传入 select_best()
  • 选择用于评估的 metric

 

返回包含最佳模型及超参数的 tibble

best_dt_model <- dt_tuning %>% 
  select_best(metric = 'roc_auc')

best_dt_model

 

# A tibble: 1 x 4
cost_complexity tree_depth  min_n  .config
     <dbl>         <int>    <int>   <chr>  
0.0000000758        14       39     Model1
在 R 中使用 tidymodels 建模

最终定型 workflow

finalize_workflow() 将含有可调参模型对象的 workflow 最终定型

  • 传入 workflow 对象
  • 一行最终超参数值的 tibble
    • 列名须与模型对象中的超参数一致

 

返回已设置超参数的 workflow 对象

final_leads_wkfl <- leads_tune_wkfl %>% 
  finalize_workflow(best_dt_model)

final_leads_wkfl
== Workflow ========================================
Preprocessor: Recipe
Model: decision_tree()
-- Preprocessor ------------------------------------
3 Recipe Steps
* step_corr()
* step_normalize()
* step_dummy()
-- Model --------------------------------------------
Decision Tree Model Specification (classification)
Main Arguments:
  cost_complexity = 0.0000000758
  tree_depth = 14
  min_n = 39
Computational engine: rpart
在 R 中使用 tidymodels 建模

模型拟合

最终的 workflow 可用 last_fit() 与原始数据划分对象 leads_split 进行训练

 

幕后流程

  • 生成训练集与测试集
  • 训练并应用 recipe
  • 用整个训练集训练调优后的决策树
  • 在测试集上计算预测与指标
leads_final_fit <- final_leads_wkfl %>% 
  last_fit(split = leads_split)

leads_final_fit %>% collect_metrics()

 

# A tibble: 2 x 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.771
2 roc_auc  binary         0.793
在 R 中使用 tidymodels 建模

Passons à la pratique !

在 R 中使用 tidymodels 建模

Preparing Video For Download...