特徵工程

在 R 中使用 tidymodels 建立模型

David Svancer

Data Scientist

使用 recipes 套件做特徵工程

使用 recipes 的特徵工程流程

在 R 中使用 tidymodels 建立模型

指定變數型別與角色

 

定義欄位角色

  • 將所有變數指定為結果或預測因子

判定變數資料型別

  • 數值資料
  • 類別資料

 

使用 recipe() 函式完成

 

用 recipe 函式指定配方

在 R 中使用 tidymodels 建立模型

資料前處理步驟

加入必要的資料前處理步驟

  • 遺漏值插補
  • 資料轉換
    • 數值變數中心化與標準化
  • 建立新變數
    • 計算變數比值
  • 以及更多…

每個步驟以對應的 step_*() 函式加入

 

用 step 函式加入資料轉換

1 https://recipes.tidymodels.org/reference/index.html
在 R 中使用 tidymodels 建立模型

訓練前處理步驟

recipe 物件會以資料來源訓練,通常是訓練資料集

  • 估計所需的資料轉換
    • 針對中心化與標準化,計算數值欄位的平均與標準差
    • 建立新欄位的公式會儲存,以便套用到新資料

 

使用 prep() 函式訓練配方

 

prep 會用訓練資料集訓練特徵工程步驟

在 R 中使用 tidymodels 建立模型

將配方套用到新資料

套用所有已訓練的資料前處理轉換

  • 用於建模的訓練與測試資料集
  • 用於未來預測的新資料來源
    • 機器學習演算法在預測新值時,需要與訓練時相同的資料格式

 

使用 bake() 函式套用配方

 

bake 會將已訓練的配方套用到新資料來源

在 R 中使用 tidymodels 建立模型

簡單的特徵工程流程

在名單評分資料中對 total_time 做對數轉換

  • 常見於數值幅度大的資料
  • 壓縮數值範圍並降低變異
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 建立模型

建立 recipe 物件

recipe() 函式

  • 模型公式
    • 指定變數角色
  • data 參數
    • 判定變數資料型別

 

recipe 物件傳給 step_log() 以加入對數轉換步驟

  • 選擇要轉換的變數 total_time,並指定對數底數
leads_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
在 R 中使用 tidymodels 建立模型

探索變數角色與型別

recipe 物件傳給 summary() 函式

  • 產生含變數資訊的 tibble
  • type
    • 紀錄變數的資料型別
    • 'nominal' 表示類別變數
  • role
    • 紀錄變數在建模中的角色
    • 依輸入的模型公式指派
leads_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
在 R 中使用 tidymodels 建立模型

訓練 recipe 物件

prep() 函式

  • 第一個參數為 recipe 物件
  • training 參數
    • 指定要用來訓練前處理步驟的資料

 

列印已訓練的 recipe 物件

  • 已訓練的步驟會標示為 [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]
在 R 中使用 tidymodels 建立模型

轉換訓練資料

bake() 函式

  • 第一個參數是已訓練的 recipe 物件
  • new_data 參數
    • 要套用已訓練 recipe 的資料
  • 訓練資料
    • leads_training 用來訓練該 recipe
    • 預設 prep() 會保留轉換後的資料
    • new_data 設為 NULL 以取出
  • 回傳含轉換後資料的 tibble
leads_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
在 R 中使用 tidymodels 建立模型

轉換新資料

轉換未用於 recipe 訓練的資料集

  • 將資料集傳入 new_data 參數
  • 已訓練的 recipe 會把所有步驟套用到新資料來源
leads_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
在 R 中使用 tidymodels 建立模型

開始 bake 吧!

在 R 中使用 tidymodels 建立模型

Preparing Video For Download...