Automatizzare il workflow di modellazione

Modellazione con tidymodels in R

David Svancer

Data Scientist

Snellire il workflow

La funzione last_fit()

  • Accetta anche modelli di classificazione
  • Velocizza il processo di modellazione
  • Adatta il modello ai dati di training e produce predizioni sul test set

 

Come con fit(), i primi passi includono:

  • Creare uno split dei dati con rsample
  • Specificare un modello con parsnip
leads_split <- initial_split(leads_df, 
                             strata = purchased)

logistic_model <- logistic_reg() %>% set_engine('glm') %>% set_mode('classification')
Modellazione con tidymodels in R

Adattare il modello e raccogliere metriche

La funzione last_fit()

  • Oggetto modello parsnip
  • Formula del modello
  • Oggetto di split dei dati

 

La funzione collect_metrics() calcola le metriche sul test set

  • Di default: Accuracy e ROC AUC
logistic_last_fit <- logistic_model %>% 
  last_fit(purchased ~ total_visits + total_time,
           split = leads_split)

logistic_last_fit %>% collect_metrics()

 

# A tibble: 2 x 3
  .metric  .estimator .estimate
  <chr>      <chr>       <dbl>
1 accuracy   binary      0.759
2 roc_auc    binary      0.763
Modellazione con tidymodels in R

Raccogliere le predizioni

collect_predictions()

  • Crea una tibble con tutte le colonne necessarie per le funzioni yardstick
  • Esiti reali e predetti sul test set
  • Colonne di probabilità stimate per tutte le classi
last_fit_results <- logistic_last_fit %>% 
  collect_predictions()
last_fit_results
# A tibble: 332 x 6
   id             .pred_yes .pred_no .row .pred_class purchased
   <chr>            <dbl>    <dbl>   <int>   <fct>      <fct>
 1 train/test split  0.134    0.866     2      no        no
 2 train/test split  0.729    0.271    17      yes       yes
 3 train/test split  0.133    0.867    21      no        no
 4 train/test split  0.0916   0.908    22      no        no
 5 train/test split  0.598    0.402    24      yes       yes
# ... with 327 more rows
Modellazione con tidymodels in R

Set di metriche personalizzate

La funzione metric_set()

  • accuracy(), sens() e spec()
    • Richiedono gli argomenti truth ed estimate
  • roc_auc()
    • Richiede truth e una colonna di probabilità stimate

 

La funzione custom_metrics() richiederà tutte e tre, con .pred_yes come ultimo argomento

custom_metrics <- metric_set(accuracy, sens,
                             spec, roc_auc)
custom_metrics(last_fit_results,
               truth = purchased,
               estimate = .pred_class,
               .pred_yes)
# A tibble: 4 x 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.759
2 sens     binary         0.617
3 spec     binary         0.840
4 roc_auc  binary         0.763
Modellazione con tidymodels in R

Ayo berlatih!

Modellazione con tidymodels in R

Preparing Video For Download...