コースへようこそ

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

Sandro Raabe

Data Scientist

コースの概要

 

  • 第1章: 分類木
  • 第2章: 回帰木、交差検証、バイアス-バリアンスのトレードオフ
  • 第3章: ハイパーパラメータ調整、バギング、ランダムフォレスト
  • 第4章: 勾配ブースティング木
Rで学ぶTree-Based ModelsによるMachine Learning

決定木はフローチャート

animal_flowchart

1 https://aca.edu.au/resources/decision-trees-classifying-animals/decision-trees.pdf
Rで学ぶTree-Based ModelsによるMachine Learning

木モデルの利点

  • 説明しやすく理解しやすい
  • 非線形関係を捉えられる
  • 数値特徴量の正規化・標準化が不要
  • ダミー変数の作成が不要
  • 外れ値に頑健
  • 大規模データでも高速
Rで学ぶTree-Based ModelsによるMachine Learning

木モデルの欠点

  • 大きい・深い・アンサンブルでは解釈が難しい
  • 高分散。複雑な木は過学習しやすい
Rで学ぶTree-Based ModelsによるMachine Learning

tidymodels_screenshot

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

tidymodels パッケージ

library(tidymodels)
-- Attaching packages -------------------- tidymodels 0.1.4 --
v parsnip   0.2.1      v rsample   0.1.1 
v dplyr     1.0.9      v tibble    3.1.7 
v yardstick 0.0.9      v tune      0.1.6
Rで学ぶTree-Based ModelsによるMachine Learning

決定木を作成する

仕様/関数型デザイン

 1. モデルクラスを選ぶ

library(tidymodels)

decision_tree()
Decision Tree Model Specification (unknown)
Rで学ぶTree-Based ModelsによるMachine Learning

決定木を作成する

 2. モデルを動かすエンジンを設定

library(tidymodels)

decision_tree() %>% 
    set_engine("rpart")
Decision Tree Model Specification (unknown)

Computational engine: rpart
Rで学ぶTree-Based ModelsによるMachine Learning

決定木を作成する

 3. モードを設定(分類 or 回帰)

library(tidymodels)

decision_tree() %>% 
     set_engine("rpart") %>% 
     set_mode("classification")
Decision Tree Model Specification (classification)

Computational engine: rpart
Rで学ぶTree-Based ModelsによるMachine Learning

仕様から実モデルへ

仕様は骨組み。学習にはデータが必要
library(tidymodels)
tree_spec <- decision_tree() %>% 
               set_engine("rpart") %>% 
               set_mode("classification")
# モデル仕様を、式と学習データで適合させる
tree_spec %>%           
  fit(formula = outcome ~ age + bmi,  
      data = diabetes)
parsnip model object
Fit time: 19 ms 
n = 652
Rで学ぶTree-Based ModelsによるMachine Learning

モデルを作ってみましょう!

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

Preparing Video For Download...