Tạo đặc trưng mới bằng hiểu biết miền

Kỹ thuật đặc trưng (Feature Engineering) với R

Jorge Zazueta

Research Professor and Head of the Modeling Group at the School of Economics, UASLP

Tầm quan trọng của hiểu biết miền

Hiểu biết miền giúp xác định và tạo các đặc trưng phù hợp cho mô hình hay tác vụ.

Kỹ thuật đặc trưng là tạo đặc trưng đầu vào mới từ dữ liệu sẵn có.

Ví dụ về hiểu biết miền:

  • Tài chính: Yếu tố quyết định phá sản
  • Y khoa: Bệnh nền liên quan đến một điều trị
  • Marketing: Đặc điểm nổi bật của nhóm khách hàng
Kỹ thuật đặc trưng (Feature Engineering) với R

Tạo biến từ kinh nghiệm chuyên môn

Ta muốn dự đoán hủy đặt phòng dựa trên vector đặc trưng sau:

features <- 
c("IsCanceled", "LeadTime",
  "arrival_date",
  "StaysInWeekendNights",
  "StaysInWeekNights",
  "PreviousCancellations",
  "PreviousBookingsNotCanceled",
  "ReservedRoomType",
  "AssignedRoomType","BookingChanges",
  "DepositType","CustomerType",
  "ADR","TotalOfSpecialRequests")

Đặc trưng từ dữ liệu thô

Ta có thể tạo đặc trưng hữu ích từ arrival_date.

Ngày đến có thể tách thành thứ, tuần, tháng và ngày lễ

Nhưng làm thủ công sẽ nhanh chóng tẻ nhạt. Cần tự động hóa!

Kỹ thuật đặc trưng (Feature Engineering) với R

Khung làm việc tidymodels

Chúng ta sẽ dùng workflow dựa trên tidymodels, bộ gói cho mô hình và ML theo nguyên tắc tidyverse (1), nhấn mạnh vào kỹ thuật đặc trưng.

Workflow tidymodels đơn giản: nạp dữ liệu, khai báo mô hình, chia dữ liệu, thiết lập recipe, gộp vào workflow, fit và đánh giá.

Tìm hiểu thêm tại www.tidymodels.org

1 [Nguyên tắc của Tidyverse.](https://design.tidyverse.org/unifying-principles.html)
Kỹ thuật đặc trưng (Feature Engineering) với R

Chuẩn bị dữ liệu cho phân tích

Hãy bắt đầu chuẩn bị dữ liệu.

cancelations <- 
  cancelations %>% 
  mutate(across(where(is_character),as.factor))
set.seed(123)
split <- cancellations %>% 
    initial_split(
    strata = "IsCanceled")
train <- training(split)
test <- testing(split)

Tham số prop dùng để đổi tỷ lệ train/test (mặc định 3/4).

initial_split(data, prop = 3/4, strata = NULL)

Xác minh rằng các tập traintest có tỷ lệ hủy đặt phòng tương tự.

train %>% 
  select(IsCanceled) %>% table() %>% 
  prop.table()

IsCanceled
        0         1 
0.5826946 0.4173054
test %>% 
  select(IsCanceled) %>% table() %>% 
  prop.table()

IsCanceled
        0         1 
0.5827788 0.4172212
Kỹ thuật đặc trưng (Feature Engineering) với R

Xây dựng workflow

Khai báo mô hình

lr_model <- logistic_reg()

Xây dựng recipe

lr_recipe <- 
  recipe(IsCanceled ~., data = train) %>%
  update_role(Agent, new_role = "ID" ) %>%
  step_date(arrival_date, 
      features = c("dow", "week", "month")) %>%
  step_holiday(arrival_date, 
      holidays = timeDate::listHolidays("US")) %>%
  step_rm(arrival_date) %>%
  step_dummy(all_nominal_predictors())

In lr_recipe

Recipe
Inputs:

      role #variables
        ID          1
   outcome          1
 predictor         13

Operations:

Date features from arrival_date
Holiday features from arrival_date
Variables removed arrival_date
Dummy variables from all_nominal_predictors()
Kỹ thuật đặc trưng (Feature Engineering) với R

Xây dựng workflow

Gộp mô hình và recipe vào đối tượng workflow.

lr_workflow <- 
  workflow()%>%
  add_model(lr_model)%>%
  add_recipe(lr_recipe)

Fit workflow

lr_fit <- 
  lr_workflow %>%
  fit(data = train)
Kỹ thuật đặc trưng (Feature Engineering) với R

Xây dựng workflow

Dùng tidy(lr_fit) để tóm tắt mô hình.

# A tibble: 65 × 5
   term                        estimate std.error statistic   p.value
   <chr>                          <dbl>     <dbl>     <dbl>     <dbl>
 1 (Intercept)                 -1.92     0.228        -8.43 3.57e- 17
 2 LeadTime                     0.00414  0.000268     15.4  1.16e- 53
 3 StaysInWeekendNights         0.0860   0.0382        2.25 2.45e-  2
 4 StaysInWeekNights            0.0804   0.0185        4.34 1.40e-  5
 5 PreviousCancellations        2.39     0.147        16.2  2.45e- 59
 6 PreviousBookingsNotCanceled -0.440    0.0450       -9.77 1.45e- 22
 7 BookingChanges              -0.449    0.0463       -9.69 3.18e- 22
 8 ADR                          0.0104   0.000782     13.2  4.85e- 40
 9 TotalOfSpecialRequests      -0.727    0.0316      -23.0  5.29e-117
10 arrival_date_week            0.0245   0.0171        1.43 1.53e-  1
# … with 55 more rows
# ℹ Use `print(n = ...)` to see more rows
Kỹ thuật đặc trưng (Feature Engineering) với R

Đánh giá hiệu năng mô hình

Giờ hãy đánh giá hiệu năng mô hình.

lr_aug <- lr_fit %>% augment(test)

bind_rows(
  lr_aug %>% 
  roc_auc(truth = IsCanceled,.pred_0),
  lr_aug %>% 
  accuracy(truth = IsCanceled,.pred_class))
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 roc_auc  binary         0.842
2 accuracy binary         0.782
lr_aug %>%
  roc_curve(truth = IsCanceled, .pred_0) %>%
  autoplot()

Đường ROC của mô hình.

Kỹ thuật đặc trưng (Feature Engineering) với R

Ayo berlatih!

Kỹ thuật đặc trưng (Feature Engineering) với R

Preparing Video For Download...