SlideShare uma empresa Scribd logo
1 de 22
森が見たい
“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

Mais conteúdo relacionado

Mais procurados

合成変量とアンサンブル:回帰森と加法モデルの要点
合成変量とアンサンブル:回帰森と加法モデルの要点合成変量とアンサンブル:回帰森と加法モデルの要点
合成変量とアンサンブル:回帰森と加法モデルの要点Ichigaku Takigawa
 
10分でわかる主成分分析(PCA)
10分でわかる主成分分析(PCA)10分でわかる主成分分析(PCA)
10分でわかる主成分分析(PCA)Takanori Ogata
 
データサイエンス概論第一=3-3 回帰分析
データサイエンス概論第一=3-3 回帰分析データサイエンス概論第一=3-3 回帰分析
データサイエンス概論第一=3-3 回帰分析Seiichi Uchida
 
ベクトルで理解する相関係数
ベクトルで理解する相関係数ベクトルで理解する相関係数
ベクトルで理解する相関係数Satoshi MATSUURA
 
データサイエンスことはじめ
データサイエンスことはじめデータサイエンスことはじめ
データサイエンスことはじめ大貴 末廣
 
計量経済学と 機械学習の交差点入り口 (公開用)
計量経済学と 機械学習の交差点入り口 (公開用)計量経済学と 機械学習の交差点入り口 (公開用)
計量経済学と 機械学習の交差点入り口 (公開用)Shota Yasui
 
相関と因果について考える:統計的因果推論、その(不)可能性の中心
相関と因果について考える:統計的因果推論、その(不)可能性の中心相関と因果について考える:統計的因果推論、その(不)可能性の中心
相関と因果について考える:統計的因果推論、その(不)可能性の中心takehikoihayashi
 
機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料
機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料
機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料at grandpa
 
データサイエンス概論第一=1-2 データのベクトル表現と集合
データサイエンス概論第一=1-2 データのベクトル表現と集合データサイエンス概論第一=1-2 データのベクトル表現と集合
データサイエンス概論第一=1-2 データのベクトル表現と集合Seiichi Uchida
 
4 データ間の距離と類似度
4 データ間の距離と類似度4 データ間の距離と類似度
4 データ間の距離と類似度Seiichi Uchida
 
ようやく分かった!最尤推定とベイズ推定
ようやく分かった!最尤推定とベイズ推定ようやく分かった!最尤推定とベイズ推定
ようやく分かった!最尤推定とベイズ推定Akira Masuda
 
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 & imlSatoshi Kato
 
PRML読み会第一章
PRML読み会第一章PRML読み会第一章
PRML読み会第一章Takushi Miki
 
整数計画法に基づく説明可能性な機械学習へのアプローチ
整数計画法に基づく説明可能性な機械学習へのアプローチ整数計画法に基づく説明可能性な機械学習へのアプローチ
整数計画法に基づく説明可能性な機械学習へのアプローチKentaro Kanamori
 
アンサンブル木モデル解釈のためのモデル簡略化法
アンサンブル木モデル解釈のためのモデル簡略化法アンサンブル木モデル解釈のためのモデル簡略化法
アンサンブル木モデル解釈のためのモデル簡略化法Satoshi Hara
 
最適輸送の計算アルゴリズムの研究動向
最適輸送の計算アルゴリズムの研究動向最適輸送の計算アルゴリズムの研究動向
最適輸送の計算アルゴリズムの研究動向ohken
 
最適化計算の概要まとめ
最適化計算の概要まとめ最適化計算の概要まとめ
最適化計算の概要まとめYuichiro MInato
 
統計学基礎
統計学基礎統計学基礎
統計学基礎Yuka Ezura
 
クラシックな機械学習の入門  8. クラスタリング
クラシックな機械学習の入門  8. クラスタリングクラシックな機械学習の入門  8. クラスタリング
クラシックな機械学習の入門  8. クラスタリングHiroshi Nakagawa
 

Mais procurados (20)

合成変量とアンサンブル:回帰森と加法モデルの要点
合成変量とアンサンブル:回帰森と加法モデルの要点合成変量とアンサンブル:回帰森と加法モデルの要点
合成変量とアンサンブル:回帰森と加法モデルの要点
 
10分でわかる主成分分析(PCA)
10分でわかる主成分分析(PCA)10分でわかる主成分分析(PCA)
10分でわかる主成分分析(PCA)
 
データサイエンス概論第一=3-3 回帰分析
データサイエンス概論第一=3-3 回帰分析データサイエンス概論第一=3-3 回帰分析
データサイエンス概論第一=3-3 回帰分析
 
ベクトルで理解する相関係数
ベクトルで理解する相関係数ベクトルで理解する相関係数
ベクトルで理解する相関係数
 
データサイエンスことはじめ
データサイエンスことはじめデータサイエンスことはじめ
データサイエンスことはじめ
 
主成分分析
主成分分析主成分分析
主成分分析
 
計量経済学と 機械学習の交差点入り口 (公開用)
計量経済学と 機械学習の交差点入り口 (公開用)計量経済学と 機械学習の交差点入り口 (公開用)
計量経済学と 機械学習の交差点入り口 (公開用)
 
相関と因果について考える:統計的因果推論、その(不)可能性の中心
相関と因果について考える:統計的因果推論、その(不)可能性の中心相関と因果について考える:統計的因果推論、その(不)可能性の中心
相関と因果について考える:統計的因果推論、その(不)可能性の中心
 
機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料
機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料
機械学習プロフェッショナルシリーズ輪読会 #5 異常検知と変化検知 Chapter 1 & 2 資料
 
データサイエンス概論第一=1-2 データのベクトル表現と集合
データサイエンス概論第一=1-2 データのベクトル表現と集合データサイエンス概論第一=1-2 データのベクトル表現と集合
データサイエンス概論第一=1-2 データのベクトル表現と集合
 
4 データ間の距離と類似度
4 データ間の距離と類似度4 データ間の距離と類似度
4 データ間の距離と類似度
 
ようやく分かった!最尤推定とベイズ推定
ようやく分かった!最尤推定とベイズ推定ようやく分かった!最尤推定とベイズ推定
ようやく分かった!最尤推定とベイズ推定
 
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
 
PRML読み会第一章
PRML読み会第一章PRML読み会第一章
PRML読み会第一章
 
整数計画法に基づく説明可能性な機械学習へのアプローチ
整数計画法に基づく説明可能性な機械学習へのアプローチ整数計画法に基づく説明可能性な機械学習へのアプローチ
整数計画法に基づく説明可能性な機械学習へのアプローチ
 
アンサンブル木モデル解釈のためのモデル簡略化法
アンサンブル木モデル解釈のためのモデル簡略化法アンサンブル木モデル解釈のためのモデル簡略化法
アンサンブル木モデル解釈のためのモデル簡略化法
 
最適輸送の計算アルゴリズムの研究動向
最適輸送の計算アルゴリズムの研究動向最適輸送の計算アルゴリズムの研究動向
最適輸送の計算アルゴリズムの研究動向
 
最適化計算の概要まとめ
最適化計算の概要まとめ最適化計算の概要まとめ
最適化計算の概要まとめ
 
統計学基礎
統計学基礎統計学基礎
統計学基礎
 
クラシックな機械学習の入門  8. クラスタリング
クラシックな機械学習の入門  8. クラスタリングクラシックな機械学習の入門  8. クラスタリング
クラシックな機械学習の入門  8. クラスタリング
 

Semelhante a 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 JPSercan Ahi
 
forestFloorパッケージを使ったrandomForestの感度分析
forestFloorパッケージを使ったrandomForestの感度分析forestFloorパッケージを使ったrandomForestの感度分析
forestFloorパッケージを使ったrandomForestの感度分析Satoshi Kato
 
データ解析4 確率の復習
データ解析4 確率の復習データ解析4 確率の復習
データ解析4 確率の復習Hirotaka Hachiya
 
正則化による尤度比推定法を応用した多値分類器の改良
正則化による尤度比推定法を応用した多値分類器の改良正則化による尤度比推定法を応用した多値分類器の改良
正則化による尤度比推定法を応用した多値分類器の改良MasatoKikuchi4
 
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナー
「樹木モデルとランダムフォレスト-機械学習による分類・予測-」-データマイニングセミナーKoichi Hamada
 
dplyr と purrrを用いたデータハンドリング
dplyr と purrrを用いたデータハンドリングdplyr と purrrを用いたデータハンドリング
dplyr と purrrを用いたデータハンドリングSomatori Keita
 

Semelhante a Interpreting Tree Ensembles with inTrees (6)

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を用いたデータハンドリング
 

Mais de 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 RSatoshi 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 RSatoshi Kato
 
Introduction of inspectDF package
Introduction of inspectDF packageIntroduction of inspectDF package
Introduction of inspectDF packageSatoshi Kato
 
Introduction of featuretweakR package
Introduction of featuretweakR packageIntroduction of featuretweakR package
Introduction of featuretweakR packageSatoshi Kato
 
Genetic algorithm full scratch with R
Genetic algorithm full scratch with RGenetic algorithm full scratch with R
Genetic algorithm full scratch with RSatoshi 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 RSatoshi 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 RSatoshi Kato
 
Oracle property and_hdm_pkg_rigorouslasso
Oracle property and_hdm_pkg_rigorouslassoOracle property and_hdm_pkg_rigorouslasso
Oracle property and_hdm_pkg_rigorouslassoSatoshi 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 ForestSatoshi Kato
 

Mais de Satoshi Kato (12)

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
 
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