分類模型

在 R 中使用 tidymodels 建立模型

David Svancer

Data Scientist

預測是否購買產品

分類模型用來預測類別型的結果變數

  • 預測是否購買產品
purchased total_time total_visits
yes 800 3
yes 978 7
no 220 4
no 124 5
yes 641 4

 

分類圖

在 R 中使用 tidymodels 建立模型

分類演算法

目標:在一組預測變數值上建立清楚且不重疊的區域。

  • 在每個區域內預測相同的分類結果。

 

分類圖

在 R 中使用 tidymodels 建立模型

分類演算法

目標:在一組預測變數值上建立清楚且不重疊的區域。

  • 在每個區域內預測相同的分類結果。

 

邏輯斯迴歸(Logistic Regression)

  • 常見的分類演算法,會在結果類別間建立「線性」分隔。

 

含邊界的分類

在 R 中使用 tidymodels 建立模型

潛在顧客評分資料

leads_df
# A tibble: 1,328 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                   8        100            2.67           24 direct_traffic west       
 3 no                   5        228            2.5            25 email          southeast  
 4 no                   7        481            2.33           21 organic_search west       
 5 no                   4        177            4              37 direct_traffic west       
 6 no                   2       1273            2              26 email          midwest    
 7 no                   3        711            3              28 organic_search west       
 8 no                   3        166            3              32 direct_traffic southeast  
 9 no                   3          7            3              23 organic_search west       
10 no                   6        562            6              48 organic_search southeast  
# ... with 1,318 more rows
在 R 中使用 tidymodels 建立模型

資料重抽樣

擬合模型的第一步

  • initial_split() 建立資料分割物件
  • training()testing() 建立訓練與測試資料集
leads_split <- initial_split(leads_df, 
                             prop = 0.75,
                             strata = purchased)

leads_training <- leads_split %>% training()
leads_test <- leads_split %>% testing()
在 R 中使用 tidymodels 建立模型

邏輯斯迴歸的模型規格

parsnip 中的模型規格

  • logistic_reg()
    • parsnip 中邏輯斯迴歸模型的一般介面
    • 常用引擎為 'glm'
    • 模式為 'classification'
logistic_model <- logistic_reg() %>% 

set_engine('glm') %>%
set_mode('classification')
在 R 中使用 tidymodels 建立模型

模型擬合

指定模型後,使用 fit() 來訓練模型。

  • 將模型物件傳入 fit()
  • 指定模型公式
  • 提供訓練資料於 data
logistic_fit <- logistic_model %>% 

fit(purchased ~ total_visits + total_time,
data = leads_training)
在 R 中使用 tidymodels 建立模型

預測結果類別

predict() 函式

  • new_data 指定要預測的新資料集
  • type
    • 'class' 會回傳分類預測

predict() 的標準化輸出

  1. 回傳一個 tibble
  2. type'class',回傳名為 .pred_class 的因子欄
class_preds <- logistic_fit %>%

predict(new_data = leads_test,
type = 'class')
class_preds
# A tibble: 332 x 1
   .pred_class
   <fct>      
 1 no         
 2 yes        
 3 no         
 4 no         
 5 yes 
 # ... with 327 more rows
在 R 中使用 tidymodels 建立模型

機率估計

type 設為 'prob' 可取得各結果類別的機率估計。

 

predict() 會回傳含多欄位的 tibble

  • 每個結果類別各一欄
  • 命名規則為 .pred_{outcome_category}
prob_preds <- logistic_fit %>%
  predict(new_data = leads_test,
          type = 'prob')

prob_preds
# A tibble: 332 x 2
   .pred_yes .pred_no
       <dbl>    <dbl>
 1    0.134     0.866
 2    0.729     0.271
 3    0.133     0.867
 4    0.0916    0.908
 5    0.598     0.402
# ... with 327 more rows
在 R 中使用 tidymodels 建立模型

合併結果

使用 yardstick 套件做模型評估時,需要一個結果用的 tibble。

 

可用 bind_cols() 合併測試資料中的結果變數與預測 tibbles

leads_results <- leads_test %>% 
  select(purchased) %>% 
  bind_cols(class_preds, prob_preds)
leads_results
# A tibble: 332 x 4
   purchased .pred_class .pred_yes .pred_no
   <fct>     <fct>           <dbl>    <dbl>
 1 no        no             0.134     0.866
 2 yes       yes            0.729     0.271
 3 no        no             0.133     0.867
 4 no        no             0.0916    0.908
 5 yes       yes            0.598     0.402
# ... with 327 more rows
在 R 中使用 tidymodels 建立模型

電信資料

telecom_df
# A tibble: 975 x 9
   canceled_service cellular_service avg_data_gb avg_call_mins avg_intl_mins internet_service   contract    months_with_company monthly_charges
   <fct>            <fct>               <dbl>         <dbl>        <dbl>         <fct>           <fct>               <dbl>           <dbl>
 1 yes              single_line         7.78           497         127         fiber_optic     month_to_month         7              76.4
 2 yes              single_line         9.04           336         88          fiber_optic     month_to_month         10             94.9
 3 no               single_line         10.3           262         55          fiber_optic     one_year               50             103. 
 4 yes              multiple_lines      5.08           250         107         digital         one_year               53             60.0
 5 no               multiple_lines      8.05           328         122         digital         two_year               50             75.2
 6 no               single_line         9.3            326         114         fiber_optic     month_to_month         25             95.7
 7 yes              multiple_lines      8.01           525         97          fiber_optic     month_to_month         19             83.6
 8 no               multiple_lines      9.4            312         147         fiber_optic     one_year               50             99.4
 9 yes              single_line         5.29           417         96          digital         month_to_month         8              49.8
10 no               multiple_lines      9.96           340         136         fiber_optic     month_to_month         61             106. 
# ... with 965 more rows 
在 R 中使用 tidymodels 建立模型

一起來練習吧!

在 R 中使用 tidymodels 建立模型

Preparing Video For Download...