名目型預測變數

在 R 中使用 tidymodels 建立模型

David Svancer

Data Scientist

名目型資料

編碼特徵或群組的資料

  • 無意義的順序

範例

  • 公司部門

    • 行銷、財務、技術
  • 母語

    • English、Czech、Spanish ...
  • 車款

    • SUV、sedan、compact ...
在 R 中使用 tidymodels 建立模型

轉換名目型預測變數

名目型資料必須轉成數值資料 才能建模

獨熱編碼(One-Hot Encoding)

  • 將類別值映射為 [0/1] 指示變數序列
  • 原始資料每個唯一值各有一個指示變數

 

one_hot_encoding

在 R 中使用 tidymodels 建立模型

轉換名目型預測變數

虛擬變數編碼(Dummy Variable Encoding)

  • 從原始類別中排除「一個」值
    • 有 n 個不同值時,產生(n - 1)個指示變數
  • 建模時較佳的方法
    • recipes 套件的預設

 

dummy_encoding

在 R 中使用 tidymodels 建立模型

潛在客戶評分資料(Lead scoring)

名目型預測變數:lead_sourceus_location

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
在 R 中使用 tidymodels 建立模型

建立虛擬變數

step_dummy() 函式

  • 從名目型預測變數建立虛擬變數
recipe(purchased ~ ., data = leads_training) %>%

step_dummy(lead_source, us_location) %>%
prep(training = leads_training) %>%
bake(new_data = leads_test)
# A tibble: 332 x 12
   total_visits ... lead_source_email  lead_source_organic_search  lead_source_direct_traffic  us_location_southeast ... us_location_west
       <dbl>    ...      <dbl>                 <dbl>                      <dbl>                       <dbl>                     <dbl>
1        8      ...         0                    0                          1                          0                        1
2        4      ...         0                    0                          1                          0                        0
3        3      ...         0                    1                          0                          0                        1
4        2      ...         1                    0                          0                          0                        0
5        9      ...         0                    0                          1                          0                        1

# ... with 327 more rows
在 R 中使用 tidymodels 建立模型

依型別選取欄位

使用 all_nominal()all_outcomes() 依欄位型別選取

  • -all_outcomes() 會排除名目型應變數 purchased
recipe(purchased ~ ., data = leads_training) %>%

step_dummy(all_nominal(), -all_outcomes()) %>%
prep(training = leads_training) %>%
bake(new_data = leads_test)
# A tibble: 332 x 12
   total_visits ... lead_source_email  lead_source_organic_search  lead_source_direct_traffic ... us_location_west
       <dbl>    ...      <dbl>                 <dbl>                      <dbl>                           <dbl>
1        8      ...         0                    0                          1                                 1
2        4      ...         0                    0                          1                                 0
3        3      ...         0                    1                          0                                 1
4        2      ...         1                    0                          0                                 0
5        9      ...         0                    0                          1                                 1
# ... with 327 more rows
在 R 中使用 tidymodels 建立模型

名目型預測變數的前處理

R 的建模引擎

  • 許多會自動建立虛擬變數
    • 可在不先用 step_dummy() 前處理下,直接用名目型預測變數
  • 各引擎行為不一
    • 一次性獨熱 vs 虛擬變數
    • 新變數命名方式

 

recipes 套件提供標準化流程,將名目型預測變數備妥供建模使用

在 R 中使用 tidymodels 建立模型

一起來練習吧!

在 R 中使用 tidymodels 建立模型

Preparing Video For Download...