ランダムフォレスト

Rで学ぶTree-Based ModelsによるMachine Learning

Sandro Raabe

Data Scientist

ランダムフォレスト

  • 高次元データに適する
  • 使いやすい
  • そのままで高性能
  • 多くのパッケージに実装: ranger, randomForest
  • tidymodels のインターフェース: rand_forest()parsnip パッケージ)
Rで学ぶTree-Based ModelsによるMachine Learning

アイデア

  • 基本:(バギングと同じ)ブートストラップ標本で木を学習
  • 重要な違い: 各木で予測子をランダムに選ぶ → ランダムフォレスト
Rで学ぶTree-Based ModelsによるMachine Learning

直感

ランダムフォレストの概念図

Rで学ぶTree-Based ModelsによるMachine Learning

コーディング:ランダムフォレストモデルの指定

  • 関数名: rand_forest()

【ハイパーパラメータ】

  • mtry: 各ノードで見る予測子数、既定:
    $$\left\lfloor\sqrt\text{num predictors}\right\rfloor$$
  • trees: フォレスト内の木の数
  • min_n: 許可される最小ノードサイズ
rand_forest(

mtry = 4,
trees = 500,
min_n = 10) %>%
# Set the mode set_mode("classification") %>%
# Use engine ranger or randomForest set_engine("ranger")
Rで学ぶTree-Based ModelsによるMachine Learning

コーディング:ランダムフォレストモデルの指定

spec <- rand_forest(trees = 100) %>%

set_mode("classification") %>%
set_engine("ranger")
ランダムフォレストのモデル仕様

(classification)
主な引数: trees = 100
計算エンジン: ranger
Rで学ぶTree-Based ModelsによるMachine Learning

フォレストの学習

spec %>% fit(still_customer ~ ., data = customers_train)
parsnip モデルオブジェクト

学習時間:  631ms 
Ranger の結果

ツリー数:                        100 
サンプルサイズ:                  9116 
独立変数の数:                    19 
Mtry:                             4 
ターゲットノードサイズ:          10
Rで学ぶTree-Based ModelsによるMachine Learning

変数重要度

rand_forest(mode = "classification") %>%
    set_engine("ranger", importance = "impurity") %>%

fit(still_customer ~ ., data = customers_train) %>%
vip::vip()

vip プロット

Rで学ぶTree-Based ModelsによるMachine Learning

ランダムフォレストを育てましょう!

Rで学ぶTree-Based ModelsによるMachine Learning

Preparing Video For Download...