Mô hình hóa với tidymodels trong R
David Svancer
Data Scientist
Tương quan đo độ mạnh quan hệ tuyến tính giữa hai biến số
Biến dự báo tương quan cao gần -1 hoặc 1
ggplot(leads_training,
aes(x = pages_per_visit, y = total_clicks)) +
geom_point() +
labs(title = 'Total Clicks vs Average Page Visits',
y = 'Total Clicks', x = 'Average Pages per Visit')

Tính ma trận tương quan
select_if()is.numericcor()leads_training %>%select_if(is.numeric) %>%cor()
total_visits total_time pages_per_visit total_clicks
total_visits 1.00 0.01 0.43 0.42
total_time 0.01 1.00 0.02 0.01
pages_per_visit 0.43 0.02 1.00 0.96
total_clicks 0.42 0.01 0.96 1.00
Loại bỏ đa cộng tuyến với recipes
recipe bằng hàm recipe()step_corr()threshold tương quanleads_cor_rec <- recipe(purchased ~ ., data = leads_training) %>%step_corr(total_visits, total_time, pages_per_visit, total_clicks, threshold = 0.9)leads_cor_rec
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Correlation filter on total_visits,..., total_clicks
all_outcomes()all_numeric()Chọn biến dự báo số cho các bước recipe
all_numeric() vào các hàm step_*()-all_outcomes()leads_cor_rec <- recipe(purchased ~ ., data = leads_training) %>%step_corr(all_numeric(), threshold = 0.9)leads_cor_rec
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Correlation filter on all_numeric()
prep()leads_training để huấn luyệnbake()pages_per_visit bị loại khỏi leads_testpages_per_visit cũng sẽ bị loại khỏi mọi dữ liệu về sauleads_cor_rec %>%prep(training = leads_training) %>%bake(new_data = leads_test)
# A tibble: 332 x 6
total_visits total_time total_clicks ... purchased
<dbl> <dbl> <dbl> ... <fct>
1 8 100 24 ... no
2 4 1346 22 ... yes
3 3 176 27 ... no
4 2 16 12 ... no
5 9 1022 12 ... yes
# ... with 327 more rows
Chuẩn hóa (chuẩn tâm và tỉ lệ) biến số
Biến total_time trong leads_training

Chuẩn hóa biến dự báo số với recipes
step_normalize()all_numeric()Có thể thêm nhiều hàm step_*() vào một recipe
leads_norm_rec <- recipe(purchased ~ ., data = leads_training) %>%step_corr(all_numeric(), threshold = 0.9) %>% step_normalize(all_numeric())leads_norm_rec
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Correlation filter on all_numeric()
Centering and scaling for all_numeric()
pages_per_vist được loại bỏ và các biến dự báo số được chuẩn hóa
leads_norm_rec %>%
prep(training = leads_training) %>%
bake(new_data = leads_test)
# A tibble: 332 x 6
total_visits total_time total_clicks lead_source us_location purchased
<dbl> <dbl> <dbl> <fct> <fct> <fct>
1 0.864 -0.984 -0.360 direct_traffic west no
2 -0.151 1.33 -0.506 direct_traffic northeast yes
3 -0.405 -0.843 -0.140 organic_search west no
4 -0.659 -1.14 -1.24 email midwest no
5 1.12 0.725 -1.24 direct_traffic west yes
# ... with 327 more rows
Mô hình hóa với tidymodels trong R