モデルの特徴量を減らす

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

Jorge Zazueta

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

特徴量を減らす理由

情報の少ない変数や無関係な変数を除く利点

  • バイアスをほぼ増やさず、分散を低減
  • ホールドアウトでの性能向上
  • 計算時間の短縮
  • モデルの複雑さを低減
  • 解釈性の向上
Rで学ぶ特徴量エンジニアリング

変数重要度でふるい分ける

全特徴量で学習

lr_recipe_full <-
  recipe(Loan_Status ~., data = train) %>%
  update_role(Loan_ID, new_role = "ID")

lr_workflow_full <- 
  workflow() %>%
  add_model(lr_model) %>%
  add_recipe(lr_recipe_full)

lr_fit_full <- 
  lr_workflow_full %>%
  fit(data = train)

変数重要度を可視化

lr_fit_full %>%
  extract_fit_parsnip() %>%
  vip(aesthetics = list(fill = "steelblue"))

変数重要度 変数重要度の棒グラフ。

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

数式構文で削減モデルを作る

基本の R 形式の数式で、特徴量を直接指定できます。

# レシピ作成
recipe_formula <- 
  recipe(Loan_Status ~ Credit_History + Property_Area + 
           LoanAmount, data = train)

# モデルとバンドル
workflow_formula <- # モデルとバンドル
  workflow() %>% add_model(lr_model) %>%
  add_recipe(recipe_formula)
Rで学ぶ特徴量エンジニアリング

特徴量ベクトルで削減モデルを作る

学習前に、特徴量ベクトルで列を選択できます。

# 特徴量ベクトル
features <- c("Credit_History", "Property_Area", "LoanAmount", "Loan_Status") 

# 学習用・テスト用データ
train_features <- train %>% select(all_of(features))
test_features <- test %>% select(all_of(features))

# レシピ作成しモデルとバンドル
recipe_features <- recipe(Loan_Status ~., data = train_features)
workflow_features <- workflow() %>% add_model(lr_model) %>%
  add_recipe(recipe_features) 
Rで学ぶ特徴量エンジニアリング

拡張オブジェクトの作成

両アプローチ用の拡張オブジェクト

lr_aug_formula <-
  workflow_formula %>%
  fit(data = train) %>%
  augment(new_data = test)
lr_aug_features <-
  workflow_features %>%
  fit(data = train_features) %>%
  augment(new_data = test_features)

どちらも同じ結果を返す

all_equal(lr_aug_features, 
lr_aug_formula %>%
select(all_of(features),
starts_with(".pred")))
[1] TRUE
Rで学ぶ特徴量エンジニアリング

フルモデルと削減モデルの比較

全特徴量を使用

lr_fit_full <- # ワークフローを学習
  lr_workflow_full %>%
  fit(data = train)
lr_aug_full <- # 付加
  lr_fit_full %>%
  augment(test)
lr_aug_full %>% # 評価
  class_evaluate(truth = Loan_Status, 
                 estimate = .pred_class,
                 .pred_Y)
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.842
2 roc_auc  binary         0.744

上位3特徴量のみ*

lr_fit_formula <- # ワークフローを学習
  workflow_formula %>%
  fit(train)
lr_aug_formula <- # 付加
  lr_fit_formula %>%
  augment(new_data = test)
lr_aug_formula %>% # 評価
  class_evaluate(truth = Loan_Status, 
                 estimate = .pred_class,
                 .pred_Y)
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.842
2 roc_auc  binary         0.733
Rで学ぶ特徴量エンジニアリング

Ayo berlatih!

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

Preparing Video For Download...