Modellazione con tidymodels in R
David Svancer
Data Scientist
Creare training e test set è il primo passo del modeling
Svantaggio
Tecnica di resampling per esplorare le prestazioni del modello

Tecnica di resampling per esplorare le prestazioni del modello

Eseguire una cross validation a 5 fold

Eseguire una cross validation a 5 fold

Eseguire una cross validation a 5 fold

Eseguire una cross validation a 5 fold
Cinque stime complessive delle prestazioni del modello

La funzione vfold_cv()
vstrataset.seed() prima di vfold_cv() per la riproducibilitàsplitsset.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
La funzione fit_resamples()
parsnip o un oggetto workflow resamplesmetrics
Ogni metrica è stimata 10 volte
meanleads_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
La funzione collect_metrics()
summarize = FALSE restituisce tutte le stime per ogni fold di cross validation.metric identifica la metrica.estimate dà il valore stimato per ciascun foldrs_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
collect_metrics() restituisce una tibble
dplyrrs_metrics.metricsummarize()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
I modelli allenati con fit_resamples() non possono generare previsioni su nuovi dati
predict() non accetta oggetti di tipo resampleScopo di 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')"
Modellazione con tidymodels in R