名義予測子

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

David Svancer

Data Scientist

名義データ

特性やグループを表すデータ

  • 有意な順序はありません

  • 会社の部門

    • マーケティング、財務、技術
  • 母語

    • 英語、チェコ語、スペイン語 など
  • 車種

    • SUV、セダン、コンパクト など
R での tidymodels によるモデリング

名義予測子の変換

モデリングのために名義データは数値に変換する必要があります ワンホットエンコーディング

  • カテゴリ値を [0/1] の指示変数に写像
  • 元データの各一意値に指示変数を作成

 

ワンホットエンコーディング

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

名義予測子の変換

ダミー変数エンコーディング

  • 元の値集合から「1つ」を除外
    • 区別できる値が n 個なら ( n - 1 ) 個の指示変数
  • モデリングで推奨
    • recipes パッケージのデフォルト

 

ダミーエンコーディング

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

リードスコアリングデータ

名義予測変数 - lead_sourceus_location

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 によるモデリング

ダミー変数の作成

step_dummy() 関数

  • 名義予測変数からダミー変数を作成
recipe(purchased ~ ., data = leads_training) %>%

step_dummy(lead_source, us_location) %>%
prep(training = leads_training) %>%
bake(new_data = leads_test)
# A tibble: 332 x 12
   total_visits ... lead_source_email  lead_source_organic_search  lead_source_direct_traffic  us_location_southeast ... us_location_west
       <dbl>    ...      <dbl>                 <dbl>                      <dbl>                       <dbl>                     <dbl>
1        8      ...         0                    0                          1                          0                        1
2        4      ...         0                    0                          1                          0                        0
3        3      ...         0                    1                          0                          0                        1
4        2      ...         1                    0                          0                          0                        0
5        9      ...         0                    0                          1                          0                        1

# ... with 327 more rows
R での tidymodels によるモデリング

型で列を選ぶ

all_nominal()all_outcomes() による列型選択

  • -all_outcomes() は名義の目的変数 purchased を除外します
recipe(purchased ~ ., data = leads_training) %>%

step_dummy(all_nominal(), -all_outcomes()) %>%
prep(training = leads_training) %>%
bake(new_data = leads_test)
# A tibble: 332 x 12
   total_visits ... lead_source_email  lead_source_organic_search  lead_source_direct_traffic ... us_location_west
       <dbl>    ...      <dbl>                 <dbl>                      <dbl>                           <dbl>
1        8      ...         0                    0                          1                                 1
2        4      ...         0                    0                          1                                 0
3        3      ...         0                    1                          0                                 1
4        2      ...         1                    0                          0                                 0
5        9      ...         0                    0                          1                                 1
# ... with 327 more rows
R での tidymodels によるモデリング

名義予測変数の前処理

R のモデリングエンジン

  • 多くはダミー変数を自動生成します
    • step_dummy() なしで名義予測子を使用可能
  • ただしエンジン間で一貫しません
    • ワンホット vs ダミー変数
    • 生成変数の命名

 

recipes パッケージは名義予測子の前処理を標準化します

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

Ayo berlatih!

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

Preparing Video For Download...