Mô hình hóa với tidymodels trong R
David Svancer
Data Scientist

Định nghĩa vai trò cột
Xác định kiểu dữ liệu biến
Thực hiện bằng hàm recipe()

Thêm các bước tiền xử lý cần thiết
Mỗi bước được thêm bằng một hàm step_*() riêng

Đối tượng recipe được huấn luyện trên một nguồn dữ liệu, thường là tập huấn luyện
Huấn luyện recipe bằng hàm prep()

Áp dụng mọi biến đổi tiền xử lý đã huấn luyện
Áp dụng recipe bằng hàm bake()

Log-transform total_time trong dữ liệu chấm điểm lead
leads_training
# A tibble: 996 x 7
purchased total_visits total_time pages_per_visit total_clicks lead_source us_location
<fct> <dbl> <dbl> <dbl> <dbl> <fct> <fct>
1 yes 7 1148 7 59 direct_traffic west
2 no 5 228 2.5 25 email southeast
3 no 7 481 2.33 21 organic_search west
4 no 4 177 4 37 direct_traffic west
5 no 2 1273 2 26 email midwest
# ... with 991 more rows
Hàm recipe()
data
Truyền đối tượng recipe vào step_log() để thêm bước logarit
total_time, và chỉ định cơ số logleads_log_rec <- recipe(purchased ~ ., data = leads_training) %>%step_log(total_time, base = 10)
leads_log_rec
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Operations:
Log transformation on total_time
Truyền một đối tượng recipe vào summary()
typeroleleads_log_rec %>%
summary()
# A tibble: 7 x 4
variable type role source
<chr> <chr> <chr> <chr>
1 total_visits numeric predictor original
2 total_time numeric predictor original
3 pages_per_visit numeric predictor original
4 total_clicks numeric predictor original
5 lead_source nominal predictor original
6 us_location nominal predictor original
7 purchased nominal outcome original
Hàm prep()
recipe làm đối số đầutraining
In một đối tượng recipe đã huấn luyện
[trained]leads_log_rec_prep <- leads_log_rec %>%
prep(training = leads_training)
leads_log_rec_prep
Data Recipe
Inputs:
role #variables
outcome 1
predictor 6
Training data contained 996 data points and
no missing data.
Operations:
Log transformation on total_time [trained]
Hàm bake()
recipe đã huấn luyệnnew_datarecipe đã huấn luyệnleads_training dùng để huấn luyện recipeprep()NULL vào new_data để trích xuấtleads_log_rec_prep %>%
bake(new_data = NULL)
# A tibble: 996 x 7
total_visits total_time ... us_location purchased
<dbl> <dbl> ... <fct> <fct>
1 7 3.06 ... west yes
2 5 2.36 ... southeast no
3 7 2.68 ... west no
4 4 2.25 ... west no
5 2 3.10 ... midwest no
# ... with 991 more rows
Biến đổi tập dữ liệu không dùng khi huấn luyện recipe
new_datarecipe đã huấn luyện sẽ áp dụng mọi bước cho dữ liệu mớileads_log_rec_prep %>%
bake(new_data = leads_test)
# A tibble: 332 x 7
total_visits total_time ... us_location purchased
<dbl> <dbl> ... <fct> <fct>
1 8 2 ... west no
2 4 3.13 ... northeast yes
3 3 2.25 ... west no
4 2 1.20 ... midwest no
5 9 3.01 ... west yes
# ... with 327 more rows
Mô hình hóa với tidymodels trong R