Mô hình hóa với tidymodels trong R
David Svancer
Data Scientist
Tạo tập huấn luyện và kiểm tra
initial_split()training()testing()leads_split <- initial_split(leads_df, strata = purchased)leads_training <- leads_split %>% training()leads_test <- leads_split %>% testing()
Đặc tả mô hình với parsnip
logistic_reg()set_engine()set_mode()purchased là biến outcome phân loại danh mụclogistic_model <- logistic_reg() %>%set_engine('glm') %>%set_mode('classification')
Mô hình Hồi quy Logistic
Đặc tả (classification)
Engine tính toán: glm
Chỉ định các bước kỹ thuật đặc trưng với recipes
recipe()step_*()leads_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
Công thức dữ liệu (Data Recipe)
Đầu vào:
vai trò #biến
outcome 1
predictor 6
Thao tác:
Bộ lọc tương quan trên all_numeric()
Chuẩn hóa (center, scale) cho all_numeric()
Biến giả từ all_nominal(), -all_outcomes()
Huấn luyện các bước kỹ thuật đặc trưng trên dữ liệu huấn luyện
prep()recipe vào prep()leads_training làm dữ liệu huấn luyệnleads_recipe_prep <- leads_recipe %>%
prep(training = leads_training)
leads_recipe_prep
Công thức dữ liệu (Data Recipe)
Đầu vào:
vai trò #biến
outcome 1
predictor 6
Dữ liệu huấn luyện có 996 điểm dữ liệu
và không có thiếu dữ liệu.
Thao tác:
Bộ lọc tương quan đã loại pages_per_visit [đã huấn luyện]
Chuẩn hóa (center, scale) cho total_visits ... [đã huấn luyện]
Biến giả từ lead_source, us_location [đã huấn luyện]
Áp dụng recipe đã huấn luyện cho dữ liệu huấn luyện và lưu kết quả để fit mô hình
leads_training_prep <- leads_recipe_prep %>% bake(new_data = NULL)leads_training_prep
# A tibble: 996 x 11
total_visits total_time ... lead_source_email lead_source_organic_search ... us_location_west
<dbl> <dbl> <dbl> <dbl> <dbl>
1 0.611 0.958 ... 0 0 ... 1
2 0.103 -0.747 ... 1 0 ... 0
3 0.611 -0.278 ... 0 1 ... 1
4 -0.151 -0.842 ... 0 0 ... 1
5 -0.659 1.19 ... 1 0 ... 0
# ... với 991 hàng nữa
Áp dụng recipe đã huấn luyện cho dữ liệu kiểm tra và lưu kết quả để đánh giá mô hình
leads_test_prep <- leads_recipe_prep %>%
bake(new_data = leads_test)
leads_test_prep
# A tibble: 332 x 11
total_visits total_time ... lead_source_email lead_source_organic_search ... us_location_west
<dbl> <dbl> <dbl> <dbl> <dbl>
1 0.864 -0.984 ... 0 0 ... 1
2 -0.151 1.33 ... 0 0 ... 0
3 -0.405 -0.843 ... 0 1 ... 1
4 -0.659 -1.14 ... 1 0 ... 0
5 1.12 0.725 ... 0 0 ... 1
# ... với 327 hàng nữa
Huấn luyện hồi quy logistic với fit()
leads_training_prep
Lấy dự đoán mô hình bằng predict()
leads_test_preplogistic_fit <- logistic_model %>%
fit(purchased ~ .,
data = leads_training_prep)
class_preds <- predict(logistic_fit,
new_data = leads_test_prep,
type = 'class')
prob_preds <- predict(logistic_fit,
new_data = leads_test_prep,
type = 'prob')
Kết hợp dự đoán thành một bộ kết quả cho các hàm metric của yardstick
purchased từ tập kiểm trabind_cols()leads_results <- leads_test %>% select(purchased) %>%bind_cols(class_preds, prob_preds)leads_results
# A tibble: 332 x 4
purchased .pred_class .pred_yes .pred_no
<fct> <fct> <dbl> <dbl>
1 no no 0.257 0.743
2 yes yes 0.896 0.104
3 no no 0.0852 0.915
4 no no 0.183 0.817
5 yes yes 0.776 0.224
# ... với 327 hàng nữa
Đánh giá hiệu năng mô hình với yardstick
yardstick để đánh giá mô hìnhleads_results %>%
conf_mat(truth = purchased,
estimate = .pred_class)
Thực tế
Dự đoán yes no
yes 77 34
no 43 178
Mô hình hóa với tidymodels trong R