特徴量エンジニアリング

R での tidymodels によるモデリング

David Svancer

Data Scientist

recipes パッケージでの特徴量エンジニアリング

recipes による特徴量エンジニアリングのワークフロー

R での tidymodels によるモデリング

変数の型と役割の指定

 

列の役割を定義

  • すべての変数に outcome か predictor を割り当て

変数の型を判定

  • 数値データ
  • カテゴリデータ

 

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 引数
    • 変数のデータ型を決定

 

step_log()recipe を渡して対数変換ステップを追加

  • 変換する変数 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 によるモデリング

変数の役割と型を確認

summary()recipe オブジェクトを渡す

  • 変数情報の 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() 関数

  • 第1引数は 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() 関数

  • 第1引数は学習済み recipe
  • new_data 引数
    • 適用対象のデータ
  • 学習データ
    • leads_training でレシピを学習
    • 既定で変換後のデータは prep() が保持
    • 取得には new_dataNULL を渡す
  • 変換後の 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 によるモデリング

さあ、bake しましょう!

R での tidymodels によるモデリング

Preparing Video For Download...