特征工程

在 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() 函数

  • 模型公式
    • 指定变量角色
  • 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 建模

训练配方对象

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 参数
    • 要应用已训练配方的数据
  • 训练数据
    • leads_training 用于训练该配方
    • 默认 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 建模

转换新数据

转换未用于配方训练的数据集

  • 将数据集传入 new_data 参数
  • 已训练的配方会将所有步骤应用到新数据源
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 建模

开始烘焙!

在 R 中使用 tidymodels 建模

Preparing Video For Download...