Välja den bästa modellen

Modellering med tidymodels i R

David Svancer

Data Scientist

Detaljerade justeringsresultat

Funktionen collect_metrics() returnerar sammanfattade resultat som standard

  • Ange summarize = FALSE för att få alla resultat från hyperparameterjustering
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
Modellering med tidymodels i R

Utforska justeringsresultat

Att ange summarise = FALSE i collect_metrics() returnerar en tibble

  • Enkelt att utforska resultat med dplyr
  • Utforska ROC AUC
    • Välj måttet roc_auc
    • Gruppera efter kolumnen id
    • Beräkna sammanfattande statistik för .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
Modellering med tidymodels i R

Visa de bäst presterande modellerna

Funktionen show_best()

  • Visar de n bäst presterande modellerna baserat på medelvärdet av metric
  • Model1 är vinnaren
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
Modellering med tidymodels i R

Välja en modell

Funktionen select_best()

  • Skicka dt_tuning-resultaten till select_best()
  • Välj det metric som ska användas för att utvärdera prestanda

 

Returnerar en tibble med den bäst presterande modellen och dess hyperparametervärden

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
Modellering med tidymodels i R

Slutföra arbetsflödet

Funktionen finalize_workflow() slutför ett workflow som innehåller ett modellobjekt med justeringsparametrar

  • Ange workflow-objektet
  • En tibble med en rad av slutliga hyperparametervärden
    • Kolumnnamnen måste matcha hyperparametrarna i modellobjektet

 

Returnerar ett workflow-objekt med fastställda hyperparametervärden

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
Modellering med tidymodels i R

Modellanpassning

Det slutförda workflow-objektet kan tränas med last_fit() och det ursprungliga datasplitsobjektet leads_split

 

I bakgrunden

  • Tränings- och testdataset skapas
  • recipe tränas och tillämpas
  • Justerat beslutsträd tränas på hela träningsdatasetet
  • Prediktioner och mätvärden beräknas på testdata
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
Modellering med tidymodels i R

Nu kör vi en övning!

Modellering med tidymodels i R

Preparing Video For Download...