Modellering med tidymodels i R
David Svancer
Data Scientist
Beslutsträd delar upp prediktorutrymmet i rektangulära regioner
Rekursiv binär uppdelning

Beslutsträd delar upp prediktorutrymmet i rektangulära regioner
Rekursiv binär uppdelning

Beslutsträd delar upp prediktorutrymmet i rektangulära regioner
Rekursiv binär uppdelning

Beslutsträd delar upp prediktorutrymmet i rektangulära regioner
Rekursiv binär uppdelning

Beslutsträd delar upp prediktorutrymmet i rektangulära regioner
Rekursiv binär uppdelning
Skapar distinkta rektangulära regioner


Interna noder visas som streckade linjer och terminalnoder som markerade rektangulära regioner

Modellspecifikation i parsnip
decision_tree()parsnip'rpart''classification' eller 'regression''classification'dt_model <- decision_tree() %>%set_engine('rpart') %>%set_mode('classification')
Datatransformationer för lead scoring-data
recipe-objektTvå R-objekt att hantera
parsnip-modell och recipe-specifikationleads_recipe <- recipe(purchased ~ ., data = leads_training) %>%step_corr(all_numeric(), threshold = 0.9) %>% step_normalize(all_numeric()) %>% step_dummy(all_nominal(), -all_outcomes())
leads_recipe
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Correlation filter on all_numeric()
Centering and scaling for all_numeric()
Dummy variables from all_nominal(), -all_outcomes()
Paketet workflows är utformat för att effektivisera modellprocessen
parsnip-objekt och ett recipe-objekt till ett enda workflow-objekt
Initieras med funktionen workflow()
add_model()recipe-objekt med add_recipe()recipeleads_wkfl <- workflow() %>%add_model(dt_model) %>%add_recipe(leads_recipe)leads_wkfl
== Workflow =====================
Preprocessor: Recipe
Model: decision_tree()
-- Preprocessor -----------------
3 Recipe Steps
* step_corr()
* step_normalize()
* step_dummy()
-- Model --------------------------
Decision Tree Model Specification (classification)
Computational engine: rpart
Träna ett workflow-objekt
workflow till last_fit() och ange datadelningsobjektetcollect_metrics()I bakgrunden
recipe tränas och tillämpasleads_wkfl_fit <- leads_wkfl %>% last_fit(split = leads_split)leads_wkfl_fit %>% collect_metrics()
# A tibble: 2 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy binary 0.771
2 roc_auc binary 0.775
Ett workflow tränat med last_fit() kan skickas till collect_predictions()
yardstick-funktioner för att utforska anpassade mätvärdenleads_wkfl_preds <- leads_wkfl_fit %>% collect_predictions()leads_wkfl_preds
# A tibble: 332 x 6
id .pred_yes .pred_no .row .pred_class purchased
<chr> <dbl> <dbl> <int> <fct> <fct>
train/test split 0.120 0.880 2 no no
train/test split 0.755 0.245 17 yes yes
train/test split 0.120 0.880 21 no no
train/test split 0.120 0.880 22 no no
train/test split 0.755 0.245 24 yes yes
# ... with 327 more rows
Skapa en anpassad måttuppsättning med metric_set()
Skicka prediktorsdataset till leads_metrics() för att beräkna måtten
leads_metrics <- metric_set(roc_auc, sens, spec)leads_wkfl_preds %>% leads_metrics(truth = purchased, estimate = .pred_class, .pred_yes)
# A tibble: 3 x 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 sens binary 0.75
2 spec binary 0.783
3 roc_auc binary 0.775
Finansiella data för konsumentlån på en bank
loan_default
loans_df
# A tibble: 872 x 8
loan_default loan_purpose missed_payment_2_yr loan_amount interest_rate installment annual_income debt_to_income
<fct> <fct> <fct> <int> <dbl> <dbl> <dbl> <dbl>
no debt_consolidation no 25000 5.47 855. 62823 39.4
yes medical no 10000 10.2 364. 40000 24.1
no small_business no 13000 6.22 442. 65000 14.0
no small_business no 36000 5.97 1152. 125000 8.09
yes small_business yes 12000 11.8 308. 65000 20.1
# ... with 867 more rows
Modellering med tidymodels i R