モデリングの一連の流れ

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

David Svancer

Data Scientist

データの再サンプリング

学習用・テスト用データの作成

  • initial_split()
    • 分割オブジェクトを作成
  • training()
    • 学習データを作成
  • testing()
    • テストデータを作成
leads_split <- initial_split(leads_df, 
                             strata = purchased)

leads_training <- leads_split %>% training()
leads_test <- leads_split %>% testing()
R での tidymodels によるモデリング

モデル指定

parsnip でモデルを指定

  • logistic_reg()
    • ロジスティック回帰の汎用インターフェース
  • set_engine()
    • エンジンは 'glm'
  • set_mode()
    • purchased は名義尺度の目的変数
    • モードは 'classification'
logistic_model <- logistic_reg() %>%

set_engine('glm') %>%
set_mode('classification')
Logistic Regression Model 
Specification (classification)

Computational engine: glm
R での tidymodels によるモデリング

特徴量エンジニアリング

recipes で前処理ステップを指定

  • recipe()
    • モデル式と学習データ
  • step_*() 関数
    • 前処理を順に実行
leads_recipe <- recipe(purchased ~ .,
                       data = leads_training) %>%

step_corr(all_numeric(), threshold = 0.9) %>% step_normalize(all_numeric()) %>% step_dummy(all_nominal(), -all_outcomes())
leads_recipe
Data Recipe
Inputs:
      role #variables
   outcome          1
 predictor          6

Operations:
Correlation filter on all_numeric()
Centering and scaling for all_numeric()
Dummy variables from all_nominal(), -all_outcomes()
R での tidymodels によるモデリング

レシピの学習

学習データで特徴量エンジニアリングを学習

  • prep()
    • recipe オブジェクトを prep() に渡す
    • 学習データに leads_training を指定
leads_recipe_prep <- leads_recipe %>% 
  prep(training = leads_training)
leads_recipe_prep
Data Recipe
Inputs:
      role #variables
   outcome          1
 predictor          6
Training data contained 996 data points 
and no missing data.

Operations:
Correlation filter removed pages_per_visit [trained]
Centering and scaling for total_visits ... [trained]
Dummy variables from lead_source, us_location [trained]
R での tidymodels によるモデリング

学習データの前処理

学習済み recipe を学習データに適用し、学習用に保存

leads_training_prep <- leads_recipe_prep %>% 
  bake(new_data = NULL)

leads_training_prep
# A tibble: 996 x 11
total_visits  total_time   ... lead_source_email  lead_source_organic_search ...  us_location_west
     <dbl>      <dbl>                <dbl>                      <dbl>                    <dbl>        
 1    0.611      0.958      ...       0                          0            ...         1
 2    0.103     -0.747      ...       1                          0            ...         0
 3    0.611     -0.278      ...       0                          1            ...         1
 4   -0.151     -0.842      ...       0                          0            ...         1
 5   -0.659      1.19       ...       1                          0            ...         0 
# ... with 991 more rows
R での tidymodels によるモデリング

テストデータの前処理

学習済み recipe をテストデータに適用し、評価用に保存

leads_test_prep <- leads_recipe_prep %>% 
  bake(new_data = leads_test)

leads_test_prep
# A tibble: 332 x 11
 total_visits  total_time  ...  lead_source_email  lead_source_organic_search ...  us_location_west
     <dbl>      <dbl>                <dbl>                      <dbl>                    <dbl>        
 1    0.864     -0.984     ...        0                          0            ...         1
 2   -0.151      1.33      ...        0                          0            ...         0
 3   -0.405     -0.843     ...        0                          1            ...         1
 4   -0.659     -1.14      ...        1                          0            ...         0
 5    1.12       0.725     ...        0                          0            ...         1   
# ... with 327 more rows
R での tidymodels によるモデリング

モデル学習と予測

fit() でロジスティック回帰モデルを学習

  • 前処理済みの学習データ leads_training_prep を使用

 

predict() で予測を取得

  • 目的変数のクラスと確率を予測
  • 前処理済みのテストデータ leads_test_prep を使用
logistic_fit <- logistic_model %>% 
  fit(purchased ~ .,
      data = leads_training_prep)

 

class_preds <- predict(logistic_fit, 
                       new_data = leads_test_prep,
                       type = 'class')

prob_preds <- predict(logistic_fit, 
                      new_data = leads_test_prep,
                      type = 'prob')
R での tidymodels によるモデリング

予測結果の結合

yardstick の指標関数用に予測を結合して結果データを作成

  • テストデータから実測の目的変数 purchased を選択
  • bind_cols() で予測を結合
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.257     0.743
 2 yes       yes            0.896     0.104
 3 no        no             0.0852    0.915
 4 no        no             0.183     0.817
 5 yes       yes            0.776     0.224
# ... with 327 more rows
R での tidymodels によるモデリング

モデル評価

yardstick でモデル性能を評価

  • 結果データはすべての yardstick 指標関数で利用可能
  • 混同行列、感度、特異度などの指標
leads_results %>% 
  conf_mat(truth = purchased, 
           estimate = .pred_class)

 

          Truth
Prediction yes  no
       yes  77  34
       no   43 178
R での tidymodels によるモデリング

Let's practice!

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

Preparing Video For Download...