Mô hình hóa với tidymodels trong R
David Svancer
Data Scientist
Cây quyết định chia không gian dự báo thành các vùng dạng hình chữ nhật
Chia đôi đệ quy

Cây quyết định chia không gian dự báo thành các vùng dạng hình chữ nhật
Chia đôi đệ quy

Cây quyết định chia không gian dự báo thành các vùng dạng hình chữ nhật
Chia đôi đệ quy

Cây quyết định chia không gian dự báo thành các vùng dạng hình chữ nhật
Chia đôi đệ quy

Cây quyết định chia không gian dự báo thành các vùng dạng hình chữ nhật
Chia đôi đệ quy
Tạo các vùng chữ nhật riêng biệt


Nút trong là các đường gạch và nút cuối là các vùng chữ nhật được tô nổi bật

Đặc tả mô hình trong parsnip
decision_tree()parsnip'rpart''classification' hoặc 'regression''classification'dt_model <- decision_tree() %>%set_engine('rpart') %>%set_mode('classification')
Biến đổi dữ liệu cho chấm điểm khách hàng tiềm năng
recipeHai đối tượng R cần quản lý
parsnip và đặc tả recipeleads_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()
Gói workflows giúp tinh gọn quy trình mô hình
parsnip và đối tượng recipe thành một workflow
Khởi tạo với hàm workflow()
add_model()recipe với add_recipe()recipe đã huấn luyệnleads_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
Huấn luyện một workflow
workflow vào last_fit() và cung cấp đối tượng chia dữ liệucollect_metrics()Hậu trường
recipe được huấn luyện và áp dụngleads_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
workflow đã huấn luyện bằng last_fit() có thể truyền vào collect_predictions()
yardstick để khảo sát chỉ số tùy chỉnhleads_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
Tạo bộ chỉ số tùy chỉnh với metric_set()
Truyền tập dự đoán vào leads_metrics() để tính chỉ số
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
Dữ liệu tài chính cho khoản vay tiêu dùng tại một ngân hàng
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
Mô hình hóa với tidymodels trong R