SlideShare a Scribd company logo
森が見たい
“Interpreting Tree Ensembles with inTrees”
inTrees package (by Houtao Deng) を紹介します
第51回R勉強会@東京(#TokyoR)
ランダムフォレスト
学習データのランダムサブセットで構築した様々な決定木の集合(=森)の
予測結果 を統合する
 分類 → 多数決
 回帰 → 平均
ALL DATA
Random subset Random subset Random subset
…
特徴変数の 重要度 も評価できます
どれだけ予測力に貢献しているか
という情報をもとに特徴変数の重要度を評価する
ランダムフォレスト
学習データのランダムサブセットで構築した様々な決定木の集合(=森)の
予測結果 を統合する
 分類 → 多数決
 回帰 → 平均
ALL DATA
Random subset Random subset Random subset
…
弱学習器を統合する
わけではない
Rでランダムフォレスト
• randomForest {randomForest}
• Breiman によるCARTのアンサンブル
• Importance 算出法は Gini importance と Permutation importance
• cForest {party}
• Hothorn らのconditional treeのアンサンブル
• Importance 算出法は conditional importance
if(! require(randomForest){ install.packages("randomForest") }
iris.rf <- randomForest(Species~., data=iris, mtry = 3)
if(! require(party) ){ install.packages("party"") }
iris.cf <- cforest(Species~., data=iris, controls=cforest_control(mtry=3))
特徴変数の重要度
• {randomForest} では、importance関数が用意されている
※ varImpPlot でもok
iris.rf <- randomForest(Species~., data=iris, mtry = 3)
iris.imp <- importance(iris.rf, type=2) # 1:MeanDecreaseAccuracy / 2:MeanDecreaseGini
barplot( t(iris.imp), main=col.names(iris.imp))
弱学習器に決定木を使ってるので、せっかくだから
どういう識別をしているのか?
という情報を評価したい
どれだけ予測力に貢献しているか
という情報をもとに特徴変数の重要度を評価する
弱学習器は決定木 {randomForest}
• {randomForest} では、getTree関数が用意されている
iris.rf <- randomForest(Species~., data=iris, mtry = 3)
tree.rf <- getTree(iris.rf, 7, labelVar=TRUE)
①
②
④ ⑤
③
⑥ ⑦
⑧ ⑨
弱学習器は決定木 {party}
• {party} では、prettytree()という内部関数が利用できる
iris.cf <- cforest(Species~., data=iris, controls=cforest_control(mtry=3))
tree.cf <- party:::prettytree(cf@ensemble[[3]],
names(cf@data@get("input")))
弱学習器は決定木 {party}
• “BinaryTree”オブジェクト(S4クラス)に変換して可視化
iris.cf <- cforest(Species~., data=iris, controls=cforest_control(mtry=3))
getTreeCF <- function(cf, k=1){
nt <- new("BinaryTree");
nt@data <- cf@data;
nt@responses <- cf@responses
nt@tree <- party:::prettytree(cf@ensemble[[k]], names(cf@data@get("input")))
return(nt)
}
tree.cf <- getTreeCF(iris.cf, 17)
plot(tree.cf,type=“simple")
You can't see the forest for the trees.
• 学習後の決定木は確認できるが、結構形が違う。
• 木をひとつずつ眺めて全体の分析するのは、まず無理。
Q.“ How can I interpret the results from a random forest? “
• どういう識別をしているのか?という情報を評価したい。
1. 学習後のアンサンブル(森)の構造を要約できないか?
2. 特徴変数が【どのように】重要なのか見れないか?
A.“The "inTrees" R package might be useful.”
• http://stackoverflow.com/questions/14996619/random-forest-output-interpretation
• この人、この質問にしか答えてない
具体的には
1.森全体の要約
枝の集計と刈込により全体像を把握
2.仮説抽出
枝をトランザクションとみなしてアソシエーション分析
inTreeを使ってみる
枝群①
枝群②
枝群③
枝群④ 枝群⑤
枝の
長さ
弱学習器
(決定木)
決定木の
取出し
枝の
取出し
枝の
刈り込み
枝の
集約
枝の
要約
条件文の
アソシエーション分析
枝の
集計
枝=条件文の論理積
----2 X1==Y & X2==Y ‐> setosa
X1==Y & X2==Y & X3==Y ‐> setosa
X1==Y & X3!=Y ‐> versicolor
条件文 アウトカム
----3
----4
----5
----1
inTreeを使ってみる:
tree sampling
> require(“inTrees”)
> require(“randomForest”)
> data(iris);
> X <- iris[,1:(ncol(iris)-1)]
> target <- iris[,"Species"]
> rf <- randomForest(X, as.factor(target))
> treeList <- RF2List(rf)
 全ての決定木を順番にgetTree()する
決定木の
取出し
枝の
取出し
枝の
刈り込み
枝の
集約
枝の
要約
条件文の
アソシエーション分析
枝の
集計
inTreeを使ってみる:
extract conditions
> exec <- extractRules(treeList,X,ntree=500)
> exec[1:2,]
condition
[1,] "X[,1]<=5.45 & X[,4]<=0.8"
[2,] "X[,1]<=5.45 & X[,4]>0.8"
 取り出した決定木に含まれる
枝(条件文の組) を抽出する
決定木の
取出し
枝の
取出し
枝の
刈り込み
枝の
集約
枝の
要約
条件文の
アソシエーション分析
枝の
集計
inTreeを使ってみる:
measure rules
> ruleMetric <- getRuleMetric(exec,X,target)
> ruleMetric[1:2,]
len freq err condition pred
[1,] "2" "0.3" "0" "X[,1]<=5.45 & X[,4]<=0.8" "setosa"
[2,] "2" "0.047" "0.143" "X[,1]<=5.45 & X[,4]>0.8" "versicolor"
決定木の
取出し
枝の
取出し
枝の
刈り込み
枝の
集約
枝の
要約
条件文の
アソシエーション分析
枝の
集計
 取り出した枝の数を集計
長さ 出現割合 予測精度 アウトカム条件文
inTreeを使ってみる:
prune each rule
> ruleMetric <- pruneRule(ruleMetric,X,target)
> ruleMetric[1:2,]
len freq err condition pred
[1,] "1" "0.3“ "0" "X[,4]<=0.8" "setosa"
[2,] "2" "0.047" "0.143" "X[,1]<=5.45 & X[,4]>0.8" "versicolor"
決定木の
取出し
枝の
取出し
枝の
刈り込み
枝の
集約
枝の
要約
条件文の
アソシエーション分析
枝の
集計
X1==Y & X2==Y ‐> setosa
X1==Y & X2==Y & X3==Y ‐> setosa
X1==Y & X3!=Y ‐> versicolor
余計な条件文を削除
浅い条件文=上位互換削除
枝が短くなった
inTreeを使ってみる:
select a compact rule set
> ruleMetric <- selectRuleRRF(ruleMetric,X,target
> ruleMetric[1:2,]
len freq err condition pred
[1,] "1" "0.333" "0" "X[,4]<=0.8" "setosa"
[2,] "2" "0.047" "0.143" "X[,1]<=5.45 & X[,4]>0.8" "versicolor"
決定木の
取出し
枝の
取出し
枝の
刈り込み
枝の
集約
枝の
要約
条件文の
アソシエーション分析
枝の
集計
X1==Y & X2==Y ‐> setosa
X1==Y & X2==Y (削除済) ‐> setosa
X1==Y & X3!=Y ‐> versicolor
集約
inTreeを使ってみる:
summarize rule set
> readableRules <- presentRules(ruleMetric,colnames(X))
> learner <- buildLearner(ruleMetric,X,target,minFreq=0.01)
> learner
決定木の
取出し
枝の
取出し
枝の
刈り込み
枝の
集約
枝の
要約
条件文の
アソシエーション分析
枝の
集計
枝を読みやすく加工する
 レアな枝を切り落とし、一本の決定木に要約する
inTreeを使ってみる:
extract frequent variable interactions
(つづきから)
> freqPattern <- getFreqPattern(ruleMetric)
> freqPattern <- presentRule(freqPattern, colnames(X))
> freqPattern[which(as.numeric(freqPattern[,"len"])>=2),][1:4,]
len sup conf condition pred
[1,] "2" "0.044" "0.577" "Petal.Width<=1.75 & Petal.Width>0.8" "versicolor"
[2,] "2" "0.042" "0.577" "Petal.Length>2.45 & Petal.Width<=1.75" "versicolor"
[3,] "2" "0.037" "1" "Petal.Length>4.85 & Petal.Width>1.75" "virginica"
[4,] "2" "0.031" "0.757" "Petal.Length>2.45 & Petal.Width>1.75" "virginica"
決定木の
取出し
枝の
取出し
枝の
刈り込み
枝の
集約
枝の
要約
条件文の
アソシエーション分析
枝の
集計
support: 弱学習器(木)から抽出したすべての枝のうち、
(指示度) この条件文を含んでいる枝の割合
confidence: この条件文を含んだすべての枝のうち、
(確信度) アウトカムを正しく識別した枝の割合
※ 刈り込みと集約はしない
1つの枝=
1つのバスケット
inTreeを使ってみる:
extract frequent variable interactions
データによっては
複雑な枝も頻出する
frequent patterns in UCI data
(開発者の論文より)
まとめ:
inTreeパッケージ試してみた
• 学習後のアンサンブル(森)の構造を見れないか?
☑ 弱学習器(木)がもつ枝の集約ができる
• 実務データだと、なかなか浅い枝では集約は難しい。
• かといって、深い枝を許すと収拾がつかなくなる。
• そもそもきれいに集約できるデータならCARTあたりで…
• 特徴変数が【どのように】重要なのか見れないか?
☑ 特徴変数間の相互作用(=仮説候補)を抽出できる
• 各木がもつ枝をバスケットとみなして、森全体の識別ルールの組み合わせを
アソシエーション分析する。
• Confidence (確信度) と Support (支持度) で重要度を評価する。
• 変数(条件)同士のパターンを捕まえたいときには便利。
参考文献
• randomForest {randomForest}
• cForest {party}
• "Party on! A New, Conditional Variable Importance Measure for Random Forests Available in the party Package",
Strobl et al. 2009.
• http://epub.ub.uni-muenchen.de/9387/1/techreport.pdf
• 弱学習器の木構造を抽出する
• “How to actually plot a sample tree from randomForest::getTree()?” -- Cross Validated
• http://stats.stackexchange.com/questions/41443/how-to-actually-plot-a-sample-tree-from-
randomforestgettree
• “Party extract BinaryTree from cforest?” -- R help
• http://r.789695.n4.nabble.com/Re-Fwd-Re-Party-extract-BinaryTree-from-cforest-td3878100.html
• 弱学習器の木構造から枝を抽出する {inTrees}
• “Random forest output interpretation” -- Stack Overflow
• http://stackoverflow.com/questions/14996619/random-forest-output-interpretation
• “Interpreting Tree Ensembles with inTrees”, Houtao Deng, arXiv:1408.5456, 2014
• https://sites.google.com/site/houtaodeng/intrees

More Related Content

What's hot

8.4 グラフィカルモデルによる推論
8.4 グラフィカルモデルによる推論8.4 グラフィカルモデルによる推論
8.4 グラフィカルモデルによる推論
sleepy_yoshi
 
[DL輪読会]Deep Learning 第5章 機械学習の基礎
[DL輪読会]Deep Learning 第5章 機械学習の基礎[DL輪読会]Deep Learning 第5章 機械学習の基礎
[DL輪読会]Deep Learning 第5章 機械学習の基礎
Deep Learning JP
 
充足可能性問題のいろいろ
充足可能性問題のいろいろ充足可能性問題のいろいろ
充足可能性問題のいろいろ
Hiroshi Yamashita
 
[DL輪読会]Deep Learning 第15章 表現学習
[DL輪読会]Deep Learning 第15章 表現学習[DL輪読会]Deep Learning 第15章 表現学習
[DL輪読会]Deep Learning 第15章 表現学習
Deep Learning JP
 
Rによるベイジアンネットワーク入門
Rによるベイジアンネットワーク入門Rによるベイジアンネットワーク入門
Rによるベイジアンネットワーク入門
Okamoto Laboratory, The University of Electro-Communications
 
混合モデルとEMアルゴリズム(PRML第9章)
混合モデルとEMアルゴリズム(PRML第9章)混合モデルとEMアルゴリズム(PRML第9章)
混合モデルとEMアルゴリズム(PRML第9章)
Takao Yamanaka
 
階層ディリクレ過程事前分布モデルによる画像領域分割
階層ディリクレ過程事前分布モデルによる画像領域分割階層ディリクレ過程事前分布モデルによる画像領域分割
階層ディリクレ過程事前分布モデルによる画像領域分割
tn1031
 
[DL輪読会]Deep Learning 第2章 線形代数
[DL輪読会]Deep Learning 第2章 線形代数[DL輪読会]Deep Learning 第2章 線形代数
[DL輪読会]Deep Learning 第2章 線形代数
Deep Learning JP
 
PRML輪読#11
PRML輪読#11PRML輪読#11
PRML輪読#11
matsuolab
 
Rでisomap(多様体学習のはなし)
Rでisomap(多様体学習のはなし)Rでisomap(多様体学習のはなし)
Rでisomap(多様体学習のはなし)Kohta Ishikawa
 
統計的学習の基礎 第2章後半
統計的学習の基礎 第2章後半統計的学習の基礎 第2章後半
統計的学習の基礎 第2章後半
Prunus 1350
 
機械学習と主成分分析
機械学習と主成分分析機械学習と主成分分析
機械学習と主成分分析
Katsuhiro Morishita
 
バンディット問題について
バンディット問題についてバンディット問題について
バンディット問題について
jkomiyama
 
論文紹介 Amortized bayesian meta learning
論文紹介 Amortized bayesian meta learning論文紹介 Amortized bayesian meta learning
論文紹介 Amortized bayesian meta learning
Xiangze
 
関数データ解析の概要とその方法
関数データ解析の概要とその方法関数データ解析の概要とその方法
関数データ解析の概要とその方法
Hidetoshi Matsui
 
PRML第9章「混合モデルとEM」
PRML第9章「混合モデルとEM」PRML第9章「混合モデルとEM」
PRML第9章「混合モデルとEM」
Keisuke Sugawara
 
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
Ken'ichi Matsui
 
ノンパラベイズ入門の入門
ノンパラベイズ入門の入門ノンパラベイズ入門の入門
ノンパラベイズ入門の入門
Shuyo Nakatani
 
PRML第6章「カーネル法」
PRML第6章「カーネル法」PRML第6章「カーネル法」
PRML第6章「カーネル法」
Keisuke Sugawara
 
機械学習を用いた異常検知入門
機械学習を用いた異常検知入門機械学習を用いた異常検知入門
機械学習を用いた異常検知入門
michiaki ito
 

What's hot (20)

8.4 グラフィカルモデルによる推論
8.4 グラフィカルモデルによる推論8.4 グラフィカルモデルによる推論
8.4 グラフィカルモデルによる推論
 
[DL輪読会]Deep Learning 第5章 機械学習の基礎
[DL輪読会]Deep Learning 第5章 機械学習の基礎[DL輪読会]Deep Learning 第5章 機械学習の基礎
[DL輪読会]Deep Learning 第5章 機械学習の基礎
 
充足可能性問題のいろいろ
充足可能性問題のいろいろ充足可能性問題のいろいろ
充足可能性問題のいろいろ
 
[DL輪読会]Deep Learning 第15章 表現学習
[DL輪読会]Deep Learning 第15章 表現学習[DL輪読会]Deep Learning 第15章 表現学習
[DL輪読会]Deep Learning 第15章 表現学習
 
Rによるベイジアンネットワーク入門
Rによるベイジアンネットワーク入門Rによるベイジアンネットワーク入門
Rによるベイジアンネットワーク入門
 
混合モデルとEMアルゴリズム(PRML第9章)
混合モデルとEMアルゴリズム(PRML第9章)混合モデルとEMアルゴリズム(PRML第9章)
混合モデルとEMアルゴリズム(PRML第9章)
 
階層ディリクレ過程事前分布モデルによる画像領域分割
階層ディリクレ過程事前分布モデルによる画像領域分割階層ディリクレ過程事前分布モデルによる画像領域分割
階層ディリクレ過程事前分布モデルによる画像領域分割
 
[DL輪読会]Deep Learning 第2章 線形代数
[DL輪読会]Deep Learning 第2章 線形代数[DL輪読会]Deep Learning 第2章 線形代数
[DL輪読会]Deep Learning 第2章 線形代数
 
PRML輪読#11
PRML輪読#11PRML輪読#11
PRML輪読#11
 
Rでisomap(多様体学習のはなし)
Rでisomap(多様体学習のはなし)Rでisomap(多様体学習のはなし)
Rでisomap(多様体学習のはなし)
 
統計的学習の基礎 第2章後半
統計的学習の基礎 第2章後半統計的学習の基礎 第2章後半
統計的学習の基礎 第2章後半
 
機械学習と主成分分析
機械学習と主成分分析機械学習と主成分分析
機械学習と主成分分析
 
バンディット問題について
バンディット問題についてバンディット問題について
バンディット問題について
 
論文紹介 Amortized bayesian meta learning
論文紹介 Amortized bayesian meta learning論文紹介 Amortized bayesian meta learning
論文紹介 Amortized bayesian meta learning
 
関数データ解析の概要とその方法
関数データ解析の概要とその方法関数データ解析の概要とその方法
関数データ解析の概要とその方法
 
PRML第9章「混合モデルとEM」
PRML第9章「混合モデルとEM」PRML第9章「混合モデルとEM」
PRML第9章「混合モデルとEM」
 
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
 
ノンパラベイズ入門の入門
ノンパラベイズ入門の入門ノンパラベイズ入門の入門
ノンパラベイズ入門の入門
 
PRML第6章「カーネル法」
PRML第6章「カーネル法」PRML第6章「カーネル法」
PRML第6章「カーネル法」
 
機械学習を用いた異常検知入門
機械学習を用いた異常検知入門機械学習を用いた異常検知入門
機械学習を用いた異常検知入門
 

Similar to Interpreting Tree Ensembles with inTrees

Feature Selection with R / in JP
Feature Selection with R / in JPFeature Selection with R / in JP
Feature Selection with R / in JP
Sercan Ahi
 
forestFloorパッケージを使ったrandomForestの感度分析
forestFloorパッケージを使ったrandomForestの感度分析forestFloorパッケージを使ったrandomForestの感度分析
forestFloorパッケージを使ったrandomForestの感度分析
Satoshi Kato
 
データ解析4 確率の復習
データ解析4 確率の復習データ解析4 確率の復習
データ解析4 確率の復習
Hirotaka Hachiya
 
正則化による尤度比推定法を応用した多値分類器の改良
正則化による尤度比推定法を応用した多値分類器の改良正則化による尤度比推定法を応用した多値分類器の改良
正則化による尤度比推定法を応用した多値分類器の改良
MasatoKikuchi4
 
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー
Koichi Hamada
 
統計学基礎
統計学基礎統計学基礎
統計学基礎
Yuka Ezura
 
dplyr と purrrを用いたデータハンドリング
dplyr と purrrを用いたデータハンドリングdplyr と purrrを用いたデータハンドリング
dplyr と purrrを用いたデータハンドリング
Somatori Keita
 

Similar to Interpreting Tree Ensembles with inTrees (7)

Feature Selection with R / in JP
Feature Selection with R / in JPFeature Selection with R / in JP
Feature Selection with R / in JP
 
forestFloorパッケージを使ったrandomForestの感度分析
forestFloorパッケージを使ったrandomForestの感度分析forestFloorパッケージを使ったrandomForestの感度分析
forestFloorパッケージを使ったrandomForestの感度分析
 
データ解析4 確率の復習
データ解析4 確率の復習データ解析4 確率の復習
データ解析4 確率の復習
 
正則化による尤度比推定法を応用した多値分類器の改良
正則化による尤度比推定法を応用した多値分類器の改良正則化による尤度比推定法を応用した多値分類器の改良
正則化による尤度比推定法を応用した多値分類器の改良
 
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー
 
統計学基礎
統計学基礎統計学基礎
統計学基礎
 
dplyr と purrrを用いたデータハンドリング
dplyr と purrrを用いたデータハンドリングdplyr と purrrを用いたデータハンドリング
dplyr と purrrを用いたデータハンドリング
 

More from Satoshi Kato

How to generate PowerPoint slides Non-manually using R
How to generate PowerPoint slides Non-manually using RHow to generate PowerPoint slides Non-manually using R
How to generate PowerPoint slides Non-manually using R
Satoshi Kato
 
Dimensionality reduction with t-SNE(Rtsne) and UMAP(uwot) using R packages.
Dimensionality reduction with t-SNE(Rtsne) and UMAP(uwot) using R packages. Dimensionality reduction with t-SNE(Rtsne) and UMAP(uwot) using R packages.
Dimensionality reduction with t-SNE(Rtsne) and UMAP(uwot) using R packages.
Satoshi Kato
 
Exploratory data analysis using xgboost package in R
Exploratory data analysis using xgboost package in RExploratory data analysis using xgboost package in R
Exploratory data analysis using xgboost package in R
Satoshi Kato
 
How to use in R model-agnostic data explanation with DALEX & iml
How to use in R model-agnostic data explanation with DALEX & imlHow to use in R model-agnostic data explanation with DALEX & iml
How to use in R model-agnostic data explanation with DALEX & iml
Satoshi Kato
 
Introduction of inspectDF package
Introduction of inspectDF packageIntroduction of inspectDF package
Introduction of inspectDF package
Satoshi Kato
 
Introduction of featuretweakR package
Introduction of featuretweakR packageIntroduction of featuretweakR package
Introduction of featuretweakR package
Satoshi Kato
 
Genetic algorithm full scratch with R
Genetic algorithm full scratch with RGenetic algorithm full scratch with R
Genetic algorithm full scratch with R
Satoshi Kato
 
Intoroduction & R implementation of "Interpretable predictions of tree-based ...
Intoroduction & R implementation of "Interpretable predictions of tree-based ...Intoroduction & R implementation of "Interpretable predictions of tree-based ...
Intoroduction & R implementation of "Interpretable predictions of tree-based ...
Satoshi Kato
 
Multiple optimization and Non-dominated sorting with rPref package in R
Multiple optimization and Non-dominated sorting with rPref package in RMultiple optimization and Non-dominated sorting with rPref package in R
Multiple optimization and Non-dominated sorting with rPref package in R
Satoshi Kato
 
Deep forest (preliminary ver.)
Deep forest  (preliminary ver.)Deep forest  (preliminary ver.)
Deep forest (preliminary ver.)
Satoshi Kato
 
Introduction of "the alternate features search" using R
Introduction of  "the alternate features search" using RIntroduction of  "the alternate features search" using R
Introduction of "the alternate features search" using R
Satoshi Kato
 
Oracle property and_hdm_pkg_rigorouslasso
Oracle property and_hdm_pkg_rigorouslassoOracle property and_hdm_pkg_rigorouslasso
Oracle property and_hdm_pkg_rigorouslasso
Satoshi Kato
 
Imputation of Missing Values using Random Forest
Imputation of Missing Values using  Random ForestImputation of Missing Values using  Random Forest
Imputation of Missing Values using Random Forest
Satoshi Kato
 

More from Satoshi Kato (13)

How to generate PowerPoint slides Non-manually using R
How to generate PowerPoint slides Non-manually using RHow to generate PowerPoint slides Non-manually using R
How to generate PowerPoint slides Non-manually using R
 
Dimensionality reduction with t-SNE(Rtsne) and UMAP(uwot) using R packages.
Dimensionality reduction with t-SNE(Rtsne) and UMAP(uwot) using R packages. Dimensionality reduction with t-SNE(Rtsne) and UMAP(uwot) using R packages.
Dimensionality reduction with t-SNE(Rtsne) and UMAP(uwot) using R packages.
 
Exploratory data analysis using xgboost package in R
Exploratory data analysis using xgboost package in RExploratory data analysis using xgboost package in R
Exploratory data analysis using xgboost package in R
 
How to use in R model-agnostic data explanation with DALEX & iml
How to use in R model-agnostic data explanation with DALEX & imlHow to use in R model-agnostic data explanation with DALEX & iml
How to use in R model-agnostic data explanation with DALEX & iml
 
Introduction of inspectDF package
Introduction of inspectDF packageIntroduction of inspectDF package
Introduction of inspectDF package
 
Introduction of featuretweakR package
Introduction of featuretweakR packageIntroduction of featuretweakR package
Introduction of featuretweakR package
 
Genetic algorithm full scratch with R
Genetic algorithm full scratch with RGenetic algorithm full scratch with R
Genetic algorithm full scratch with R
 
Intoroduction & R implementation of "Interpretable predictions of tree-based ...
Intoroduction & R implementation of "Interpretable predictions of tree-based ...Intoroduction & R implementation of "Interpretable predictions of tree-based ...
Intoroduction & R implementation of "Interpretable predictions of tree-based ...
 
Multiple optimization and Non-dominated sorting with rPref package in R
Multiple optimization and Non-dominated sorting with rPref package in RMultiple optimization and Non-dominated sorting with rPref package in R
Multiple optimization and Non-dominated sorting with rPref package in R
 
Deep forest (preliminary ver.)
Deep forest  (preliminary ver.)Deep forest  (preliminary ver.)
Deep forest (preliminary ver.)
 
Introduction of "the alternate features search" using R
Introduction of  "the alternate features search" using RIntroduction of  "the alternate features search" using R
Introduction of "the alternate features search" using R
 
Oracle property and_hdm_pkg_rigorouslasso
Oracle property and_hdm_pkg_rigorouslassoOracle property and_hdm_pkg_rigorouslasso
Oracle property and_hdm_pkg_rigorouslasso
 
Imputation of Missing Values using Random Forest
Imputation of Missing Values using  Random ForestImputation of Missing Values using  Random Forest
Imputation of Missing Values using Random Forest
 

Interpreting Tree Ensembles with inTrees

  • 1. 森が見たい “Interpreting Tree Ensembles with inTrees” inTrees package (by Houtao Deng) を紹介します 第51回R勉強会@東京(#TokyoR)
  • 4. ランダムフォレスト 学習データのランダムサブセットで構築した様々な決定木の集合(=森)の 予測結果 を統合する  分類 → 多数決  回帰 → 平均 ALL DATA Random subset Random subset Random subset … 弱学習器を統合する わけではない
  • 5. Rでランダムフォレスト • randomForest {randomForest} • Breiman によるCARTのアンサンブル • Importance 算出法は Gini importance と Permutation importance • cForest {party} • Hothorn らのconditional treeのアンサンブル • Importance 算出法は conditional importance if(! require(randomForest){ install.packages("randomForest") } iris.rf <- randomForest(Species~., data=iris, mtry = 3) if(! require(party) ){ install.packages("party"") } iris.cf <- cforest(Species~., data=iris, controls=cforest_control(mtry=3))
  • 6. 特徴変数の重要度 • {randomForest} では、importance関数が用意されている ※ varImpPlot でもok iris.rf <- randomForest(Species~., data=iris, mtry = 3) iris.imp <- importance(iris.rf, type=2) # 1:MeanDecreaseAccuracy / 2:MeanDecreaseGini barplot( t(iris.imp), main=col.names(iris.imp)) 弱学習器に決定木を使ってるので、せっかくだから どういう識別をしているのか? という情報を評価したい どれだけ予測力に貢献しているか という情報をもとに特徴変数の重要度を評価する
  • 7. 弱学習器は決定木 {randomForest} • {randomForest} では、getTree関数が用意されている iris.rf <- randomForest(Species~., data=iris, mtry = 3) tree.rf <- getTree(iris.rf, 7, labelVar=TRUE) ① ② ④ ⑤ ③ ⑥ ⑦ ⑧ ⑨
  • 8. 弱学習器は決定木 {party} • {party} では、prettytree()という内部関数が利用できる iris.cf <- cforest(Species~., data=iris, controls=cforest_control(mtry=3)) tree.cf <- party:::prettytree(cf@ensemble[[3]], names(cf@data@get("input")))
  • 9. 弱学習器は決定木 {party} • “BinaryTree”オブジェクト(S4クラス)に変換して可視化 iris.cf <- cforest(Species~., data=iris, controls=cforest_control(mtry=3)) getTreeCF <- function(cf, k=1){ nt <- new("BinaryTree"); nt@data <- cf@data; nt@responses <- cf@responses nt@tree <- party:::prettytree(cf@ensemble[[k]], names(cf@data@get("input"))) return(nt) } tree.cf <- getTreeCF(iris.cf, 17) plot(tree.cf,type=“simple")
  • 10. You can't see the forest for the trees. • 学習後の決定木は確認できるが、結構形が違う。 • 木をひとつずつ眺めて全体の分析するのは、まず無理。
  • 11. Q.“ How can I interpret the results from a random forest? “ • どういう識別をしているのか?という情報を評価したい。 1. 学習後のアンサンブル(森)の構造を要約できないか? 2. 特徴変数が【どのように】重要なのか見れないか? A.“The "inTrees" R package might be useful.” • http://stackoverflow.com/questions/14996619/random-forest-output-interpretation • この人、この質問にしか答えてない 具体的には 1.森全体の要約 枝の集計と刈込により全体像を把握 2.仮説抽出 枝をトランザクションとみなしてアソシエーション分析
  • 13. inTreeを使ってみる: tree sampling > require(“inTrees”) > require(“randomForest”) > data(iris); > X <- iris[,1:(ncol(iris)-1)] > target <- iris[,"Species"] > rf <- randomForest(X, as.factor(target)) > treeList <- RF2List(rf)  全ての決定木を順番にgetTree()する 決定木の 取出し 枝の 取出し 枝の 刈り込み 枝の 集約 枝の 要約 条件文の アソシエーション分析 枝の 集計
  • 14. inTreeを使ってみる: extract conditions > exec <- extractRules(treeList,X,ntree=500) > exec[1:2,] condition [1,] "X[,1]<=5.45 & X[,4]<=0.8" [2,] "X[,1]<=5.45 & X[,4]>0.8"  取り出した決定木に含まれる 枝(条件文の組) を抽出する 決定木の 取出し 枝の 取出し 枝の 刈り込み 枝の 集約 枝の 要約 条件文の アソシエーション分析 枝の 集計
  • 15. inTreeを使ってみる: measure rules > ruleMetric <- getRuleMetric(exec,X,target) > ruleMetric[1:2,] len freq err condition pred [1,] "2" "0.3" "0" "X[,1]<=5.45 & X[,4]<=0.8" "setosa" [2,] "2" "0.047" "0.143" "X[,1]<=5.45 & X[,4]>0.8" "versicolor" 決定木の 取出し 枝の 取出し 枝の 刈り込み 枝の 集約 枝の 要約 条件文の アソシエーション分析 枝の 集計  取り出した枝の数を集計 長さ 出現割合 予測精度 アウトカム条件文
  • 16. inTreeを使ってみる: prune each rule > ruleMetric <- pruneRule(ruleMetric,X,target) > ruleMetric[1:2,] len freq err condition pred [1,] "1" "0.3“ "0" "X[,4]<=0.8" "setosa" [2,] "2" "0.047" "0.143" "X[,1]<=5.45 & X[,4]>0.8" "versicolor" 決定木の 取出し 枝の 取出し 枝の 刈り込み 枝の 集約 枝の 要約 条件文の アソシエーション分析 枝の 集計 X1==Y & X2==Y ‐> setosa X1==Y & X2==Y & X3==Y ‐> setosa X1==Y & X3!=Y ‐> versicolor 余計な条件文を削除 浅い条件文=上位互換削除 枝が短くなった
  • 17. inTreeを使ってみる: select a compact rule set > ruleMetric <- selectRuleRRF(ruleMetric,X,target > ruleMetric[1:2,] len freq err condition pred [1,] "1" "0.333" "0" "X[,4]<=0.8" "setosa" [2,] "2" "0.047" "0.143" "X[,1]<=5.45 & X[,4]>0.8" "versicolor" 決定木の 取出し 枝の 取出し 枝の 刈り込み 枝の 集約 枝の 要約 条件文の アソシエーション分析 枝の 集計 X1==Y & X2==Y ‐> setosa X1==Y & X2==Y (削除済) ‐> setosa X1==Y & X3!=Y ‐> versicolor 集約
  • 18. inTreeを使ってみる: summarize rule set > readableRules <- presentRules(ruleMetric,colnames(X)) > learner <- buildLearner(ruleMetric,X,target,minFreq=0.01) > learner 決定木の 取出し 枝の 取出し 枝の 刈り込み 枝の 集約 枝の 要約 条件文の アソシエーション分析 枝の 集計 枝を読みやすく加工する  レアな枝を切り落とし、一本の決定木に要約する
  • 19. inTreeを使ってみる: extract frequent variable interactions (つづきから) > freqPattern <- getFreqPattern(ruleMetric) > freqPattern <- presentRule(freqPattern, colnames(X)) > freqPattern[which(as.numeric(freqPattern[,"len"])>=2),][1:4,] len sup conf condition pred [1,] "2" "0.044" "0.577" "Petal.Width<=1.75 & Petal.Width>0.8" "versicolor" [2,] "2" "0.042" "0.577" "Petal.Length>2.45 & Petal.Width<=1.75" "versicolor" [3,] "2" "0.037" "1" "Petal.Length>4.85 & Petal.Width>1.75" "virginica" [4,] "2" "0.031" "0.757" "Petal.Length>2.45 & Petal.Width>1.75" "virginica" 決定木の 取出し 枝の 取出し 枝の 刈り込み 枝の 集約 枝の 要約 条件文の アソシエーション分析 枝の 集計 support: 弱学習器(木)から抽出したすべての枝のうち、 (指示度) この条件文を含んでいる枝の割合 confidence: この条件文を含んだすべての枝のうち、 (確信度) アウトカムを正しく識別した枝の割合 ※ 刈り込みと集約はしない 1つの枝= 1つのバスケット
  • 20. inTreeを使ってみる: extract frequent variable interactions データによっては 複雑な枝も頻出する frequent patterns in UCI data (開発者の論文より)
  • 21. まとめ: inTreeパッケージ試してみた • 学習後のアンサンブル(森)の構造を見れないか? ☑ 弱学習器(木)がもつ枝の集約ができる • 実務データだと、なかなか浅い枝では集約は難しい。 • かといって、深い枝を許すと収拾がつかなくなる。 • そもそもきれいに集約できるデータならCARTあたりで… • 特徴変数が【どのように】重要なのか見れないか? ☑ 特徴変数間の相互作用(=仮説候補)を抽出できる • 各木がもつ枝をバスケットとみなして、森全体の識別ルールの組み合わせを アソシエーション分析する。 • Confidence (確信度) と Support (支持度) で重要度を評価する。 • 変数(条件)同士のパターンを捕まえたいときには便利。
  • 22. 参考文献 • randomForest {randomForest} • cForest {party} • "Party on! A New, Conditional Variable Importance Measure for Random Forests Available in the party Package", Strobl et al. 2009. • http://epub.ub.uni-muenchen.de/9387/1/techreport.pdf • 弱学習器の木構造を抽出する • “How to actually plot a sample tree from randomForest::getTree()?” -- Cross Validated • http://stats.stackexchange.com/questions/41443/how-to-actually-plot-a-sample-tree-from- randomforestgettree • “Party extract BinaryTree from cforest?” -- R help • http://r.789695.n4.nabble.com/Re-Fwd-Re-Party-extract-BinaryTree-from-cforest-td3878100.html • 弱学習器の木構造から枝を抽出する {inTrees} • “Random forest output interpretation” -- Stack Overflow • http://stackoverflow.com/questions/14996619/random-forest-output-interpretation • “Interpreting Tree Ensembles with inTrees”, Houtao Deng, arXiv:1408.5456, 2014 • https://sites.google.com/site/houtaodeng/intrees