數值型預測變數

在 R 中使用 tidymodels 建立模型

David Svancer

Data Scientist

相關的預測變數

相關係數衡量兩個數值變數間線性關係的強度。

  • 範圍為 -1 到 1
  • 當預測變數高度相關,接近 -1 或 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')

潛在客戶評分資料中,每次造訪頁數與總點擊數的散佈圖

在 R 中使用 tidymodels 建立模型

尋找相關的預測變數

計算「相關矩陣」

  • 將資料集傳入 select_if() 函式
    • is.numeric 作為引數
  • 再傳入 cor() 函式
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
在 R 中使用 tidymodels 建立模型

處理相關的預測變數

recipes 去除多重共線性

  • recipe() 指定 recipe 物件
  • 傳入 step_corr()
    • 加入所有數值欄位
      • 欄位名稱以逗號分隔
    • 設定相關性 threshold
      • 以絕對值計算
      • 閥值 0.9 會移除 ≥ 0.9 或 ≤ -0.9 的相關性
leads_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
在 R 中使用 tidymodels 建立模型

依型別選取預測變數

  • all_outcomes()
    • 選取目標變數
  • all_numeric()
    • 選取所有數值變數
      • 若目標變數是數值型,也會被包含

recipe 步驟中選取數值型預測變數

  • all_numeric() 傳入 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()
在 R 中使用 tidymodels 建立模型

訓練並套用 recipe

  • 使用 prep() 訓練
    • 提供 leads_training 作為訓練資料
  • 使用 bake() 套用
    • leads_test 中的 pages_per_visit 已被移除
    • 未來所有新資料也會移除 pages_per_visit
leads_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
在 R 中使用 tidymodels 建立模型

正規化(Normalization)

數值變數的中心化與標準化

  • 減去平均數
  • 除以標準差
  • 轉換為「以標準差為單位」的資料
    • 轉換後平均為 0、標準差為 1

leads_training 中的 total_time 變數

  • 在網站停留 1,273 秒,比平均時間高出 1.19 個標準差

 

從 leads 訓練資料正規化 total_time 變數的範例

在 R 中使用 tidymodels 建立模型

組合資料前處理步驟

使用 recipes 正規化數值型預測變數

  • step_normalize()
    • 欄位名稱或 all_numeric() 選擇器
    • 使用訓練資料的平均與標準差,套用到新資料

同一個 recipe 可加入多個 step_*() 步驟

  • 先後順序很重要
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()
在 R 中使用 tidymodels 建立模型

轉換測試資料

pages_per_vist 被移除,且數值型預測變數已正規化

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

一起來練習吧!

在 R 中使用 tidymodels 建立模型

Preparing Video For Download...