一般的な特徴量変換

Rで学ぶ特徴量エンジニアリング

Jorge Zazueta

Research Professor and Head of the Modeling Group at the School of Economics, UASLP

2 つの変換族

Box-Cox

  • 非正規な変数を正規に近づけるための変換
  • 逆数、対数、平方根、立方根を含む族
  • 正の値にのみ有効

Box-Cox 変換の式。

Yeo-Johnson

  • Box-Cox と同様の性質
  • 0 や負の値も扱える
  • 正の y では Box-Cox を y+1 に適用したものと同じ

Yeo-Johnson 変換の式。

Rで学ぶ特徴量エンジニアリング

loans_num データセット

glimpse(loans_num)
Rows: 480
Columns: 6
$ Loan_Status       <fct> N, Y, Y, Y, Y, Y, N, Y, N, Y, Y, N, Y, Y, N...
$ ApplicantIncome   <dbl> 4583, 3000, 2583, 6000, 5417, 2333, 3036, 4...
$ CoapplicantIncome <dbl> 1508, 0, 2358, 0, 4196, 1516, 2504, 1526, 1...
$ LoanAmount        <dbl> 128, 66, 120, 141, 267, 95, 158, 168, 349, ...
$ Loan_Amount_Term  <dbl> 360, 360, 360, 360, 360, 360, 360, 360, 360...
$ Credit_History    <fct> 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0...
Rで学ぶ特徴量エンジニアリング

変換の適用

プレーンなレシピ

lr_recipe_plain <- # Define recipe
  recipe(Loan_Status ~., data = train)
lr_workflow_plain <- # Bundle workflows
  workflow() %>%
  add_model(lr_model) %>%
  add_recipe(lr_recipe_plain)
lr_fit_plain <- # fit and augment
  lr_workflow_plain %>%
  fit(train)

性能評価

lr_aug_plain %>% # Assess
  class_evaluate(truth = Loan_Status,
                 estimate = .pred_class,
                 .pred_N)
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.817
2 roc_auc  binary         0.641
Rで学ぶ特徴量エンジニアリング

変換の適用

Box-Cox レシピ

lr_recipe_BC <- # Define recipe
  recipe(Loan_Status ~., data = train) %>%
  step_BoxCox(all_numeric())
lr_workflow_BC <- # Bundle workflows
  workflow() %>%
  add_model(lr_model) %>%
  add_recipe(lr_recipe_BC)
lr_fit_BC <- # fit and augment
  lr_workflow_BC %>%
  fit(train)

警告メッセージ

Box-Cox は非正の値を処理できません

Warning messages:
1: Non-positive values in selected
variable. 
2: No Box-Cox transformation could be 
estimated for: `CoapplicantIncome`
Rで学ぶ特徴量エンジニアリング

変換の適用

Box-Cox レシピ(再)

警告を避けるため、CoappliantIncome の選択を外します。

lr_recipe_BC <- # Define recipe
  recipe(Loan_Status ~., data = train) %>%
  step_BoxCox(all_numeric(), 
              -CoapplicantIncome)
lr_workflow_BC <- # Bundle workflows
  workflow() %>%
  add_model(lr_model) %>%
  add_recipe(lr_recipe_BC)
lr_fit_BC <- # fit and augment
  lr_workflow_BC %>%
  fit(train)

性能評価

lr_aug_BC %>% # Assess
  class_evaluate(truth = Loan_Status,
                 estimate = .pred_class,
                 .pred_N)
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.817
2 roc_auc  binary         0.599
Rで学ぶ特徴量エンジニアリング

変換の適用

Yeo-Johnson レシピ

lr_recipe_YJ <- # Define recipe
  recipe(Loan_Status ~., data = train) %>%
  step_YeoJohnson(all_numeric())
lr_workflow_YJ <- # Bundle workflows
  workflow() %>%
  add_model(lr_model) %>%
  add_recipe(lr_recipe_YJ)
lr_fit_YJ <- # fit and augment
  lr_workflow_YJ %>%
  fit(train)

性能評価

lr_aug_YJ %>% # Assess
  class_evaluate(truth = Loan_Status,
                 estimate = .pred_class,
                 .pred_N)
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.817
2 roc_auc  binary         0.700
Rで学ぶ特徴量エンジニアリング

Ayo berlatih!

Rで学ぶ特徴量エンジニアリング

Preparing Video For Download...