More Related Content More from Hidekazu Tanaka (11) アンサンブル学習9. なぜ,アンサンブル学習は有効か?
0.2
m = 21, ✓ = 0.3
0.18
0.16
0.14
0.12
P(k)
0.1
0.08
0.06
0.04
0.02
0
0 5 10 15 20
9
k
10. なぜ,アンサンブル学習は有効か?
0.2
m = 21, ✓ = 0.3
0.18
0.16
0.14
0.12
P(k)
0.1
0.08
0.06
0.04
0.02
0
0 5 10 15 20
9
k
11. なぜ,アンサンブル学習は有効か?
0.2
m = 21, ✓ = 0.3
0.18
0.16
0.14
極めて直感的な説明であることに注意
0.12
詳細な解析はされていないらしい
P(k)
0.1
0.08
0.06
0.04
0.02
0
0 5 10 15 20
9
k
15. バギングのアルゴリズム
1.以下の手順を B 回繰り返す.
a.データからm 回復元抽出して、新しいデータを作る.
b.弱学習器 h を構築する.
2. B 個の弱学習器 h を用いて最終的な学習器を構築する.
•判別問題の場合: H(x) = argmax|{i|hi = y}|
B
X
•回帰問題の場合: H(x) = 1 hi
B i=1
13
18. AdaBoost のアルゴリズム
入力 (x1 , u1 ), . . . , (xm , ym ) where xi 2 X, yi 2 Y = { 1, +1}
初期化 重みの初期値を とする.
D1 (i) = 1/m
繰り返し t = 1, . . . , T として,以下の手順を繰り返す.
1.確率分布 Dt に従う弱学習器 ht : X ! { 1, +1} を構築する.
2.弱学習器 ht の誤り率 ✏t を計算する.
X
✏t = Prx⇠Dt [ft (xi ) 6= yi ] = Dt (i)
i:ht (xi )6=yi
3.弱学習器 ht の重要度 ↵t を計算 1 1 ✏t
↵t = log( )
4.重みを更新する. ⇢
2 ✏t
Dt (i) e ↵t if ht (xi ) = yi
Dt+1 (i) = ⇥
Zt e ↵t if ht (xi ) 6= yi
Dt (i)exp( ↵t yi ht (xi ))
=
Zt!
XT
出力 H(x) = sign ↵t ht (x) 16
t=1
23. ランダムフォレストのアルゴリズム
1.以下の手順を B 回繰り返す.
a.訓練データからブートストラップサンプルを作成する.
b.ブートストラップサンプルから決定木 Tiを構築する.このとき,
指定したノード数 nmin になるまで,以下の手順を繰り返す.
i. p 個の説明変数から m 個の変数をランダムに選択する.
ii. m 個の説明変数から最も良い変数を分岐ノードとする.
2. B 個の決定木 Ti を用いて最終的な学習器を構築する
•判別問題の場合は多数決
•回帰問題の場合は平均
21
24. アルゴリズムの補足
• パラメータ nmin と m をどう決めれば良いのか?
p
• 判別問題の場合: nmin = 1, m = p
• 回帰問題に場合: nmin = 5, m = p/3
• バギングとランダムフォレストの違いは?
• バギングでは説明変数を全て使う
• ランダムフォレストでは説明変数をランダムサンプリングして使う
22
26. OOB データとは?
• Out-Of-Bag の略
• ブートストラップサンプルから約3分の1を取り除いたデータのこと
• 残りのデータで決定木を構築する
• 決定木を構築した後に OOB データでテストを行う.その誤判定率
を OOB 推定値と呼ぶ.
24
28. DNAデータにおける説明変数の重要度
0.6
0.55
0.5
Variable Importance
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0 20 40 60 80 100 120 140 160 180
Variable
26
29. DNAデータにおける説明変数の重要度
0.6
0.55
0.5
Variable Importance
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0 20 40 60 80 100 120 140 160 180
Variable
26
31. 外れ値の特定方法
1.クラス j に属する事例 n と残りの事例の近似度の平均値を求める.
X
¯
P (n) = prox2 (n, k)
↵(k)=j
2. out(n) を以下のように定義する.
¯
out(n) = N/P (n)
3.各クラスにおける out(n) の中央値と絶対偏差を計算する.
4.最終的な結果は out(n) から中央値を引き,絶対偏差で割ったものと
する.
28
34. パッケージ
• バギング
• adabag
• ipred
• ブースティング
• ada
• adabag
• ランダムフォレスト
• randomForest
31
37. bagging 関数の実行例
> set.seed(50)
> tr.num <- sample(4601, 2500)
> spam.train <- spam[tr.num,]
> spam.test <- spam[-tr.num,]
> spam.ba <- bagging(type~., data=spam.train)
> spam.bap <- predict(spam.ba, spam.test)
> spam.bap$confusion
Observed Class
Predicted Class nonspam spam
nonspam 1208 124
spam 74 695
> spam.bap$error
[1] 0.09424084
34
38. boosting 関数
使い方
boosting(formula, data, boos = TRUE, mfinal = 100,
coeflearn = 'Breiman', control)
引数
mfinal 弱学習器の個数
重要度 ↵t の計算方法
coeflearn Breiman : ↵t = 1/2log((1 ✏t )/✏t )
Freund : ↵t = log((1 ✏t )/✏t )
control rpart.control 関数
35
39. boosting 関数の実行例
> spam.ad <- boosting(type~., data=spam.train)
> spam.adp <- predict(spam.ad, newdata=spam.test)
> spam.adp$confusion
Observed Class
Predicted Class nonspam spam
nonspam 1232 46
spam 50 773
> spam.adp$error
[1] 0.04569253
36
41. randomForest の実行例
> spam.rf <- randomForest(type~., data=spam.train,
na.action="na.omit")
> spam.rfp<-predict(spam.rf, spam.test[,-58])
> (spam.rft<-table(spam.test[,58], spam.rfp))
spam.rfp
nonspam spam
nonspam 1240 42
spam 75 744
> sum(diag(spam.rft))/sum(spam.rft)
[1] 0.9443122
38
43. 説明変数の重要度
spam.rf
> varImpPlot(spam.rf)
charExclamation charExclamation
remove charDollar
capitalAve remove
hp free
charDollar capitalAve
free capitalLong
capitalLong your
edu hp
capitalTotal money
our capitalTotal
george our
your you
num1999 num000
money george
re hpl
you edu
hpl num1999
charRoundbracket charRoundbracket
meeting business
num000 all
business internet
will will
num650 re
internet email
font over
pm receive
technology mail
num85 credit
receive address
credit num650
0.35 0.40 0.45 0.50 0.55 0.60 0.65 0 50 100 150
MeanDecreaseAccuracy MeanDecreaseGini
40
46. caret パッケージとは?
• classification and regression training の略
• 100以上の機械学習アルゴリズムを一つのスキームで評価すること
できる
• 詳細は第11回R勉強会@東京(Tokyo.R#11)の @dichika さ
んの発表資料を参照
42
47. モデルの作成と評価
> rpartFit <- train(trainDescr, trainMDRR, method="rpart",
preProcess=c("center", "scale"), trControl=fitControl)
> bagFit <- train(trainDescr, trainMDRR, method="treebag"
preProcess=c("center", "scale"), trControl=fitControl)
> adaFit <- train(trainDescr, trainMDRR, method="ada"
preProcess=c("center", "scale"), trControl=fitControl)
> rfFit <- train(trainDescr, trainMDRR, method="rf"
preProcess=c("center", "scale"), trControl=fitControl)
> allPred <- extractPrediction(list(rpartFit, bagFit, adaFit, rfFit),
testX=testDescr, testY=testMDRR)
> testPred <- subset(allPred, dataType=="Test")
> tp_rpart <- subset(testPred, model=="rpart")
> tp_bag <- subset(testPred, model=="treebag")
> tp_rf <- subset(testPred, model=="rf")
https://gist.github.com/2006235
43
48. モデルの比較①
> confusionMatrix(tp_rpar$pred, tp_rpa$obs) > confusionMatrix(tp_bag$pred, tp_bag$obs)
Confusion Matrix and Statistics Confusion Matrix and Statistics
Reference Reference
Prediction Active Inactive Prediction Active Inactive
Active 132 51 Active 129 32
Inactive 17 64 Inactive 20 83
Accuracy : 0.7424 Accuracy : 0.803
95% CI : (0.6852, 0.7941) 95% CI : (0.7499, 0.8493)
No Information Rate : 0.5644 No Information Rate : 0.5644
P-Value [Acc > NIR] : 1.514e-09 P-Value [Acc > NIR] : 2.566e-16
Kappa : 0.4579 Kappa : 0.5946
Mcnemar's Test P-Value : 6.285e-05 Mcnemar's Test P-Value : 0.1272
Sensitivity : 0.8859 Sensitivity : 0.8658
Specificity : 0.5565 Specificity : 0.7217
Pos Pred Value : 0.7213 Pos Pred Value : 0.8012
Neg Pred Value : 0.7901 Neg Pred Value : 0.8058
Prevalence : 0.5644 Prevalence : 0.5644
Detection Rate : 0.5000 Detection Rate : 0.4886
Detection Prevalence : 0.6932 Detection Prevalence : 0.6098
'Positive' Class : Active 'Positive' Class : Active 44
49. モデルの比較②
> confusionMatrix(tp_ada$pred, tp_ada$obs) > confusionMatrix(tp_rf$pred, tp_rf$obs)
Confusion Matrix and Statistics Confusion Matrix and Statistics
Reference Reference
Prediction Active Inactive Prediction Active Inactive
Active 133 33 Active 133 31
Inactive 16 82 Inactive 16 84
Accuracy : 0.8144 Accuracy : 0.822
95% CI : (0.7622, 0.8594) 95% CI : (0.7704, 0.8662)
No Information Rate : 0.5644 No Information Rate : 0.5644
P-Value [Acc > NIR] : < 2e-16 P-Value [Acc > NIR] : < 2e-16
Kappa : 0.6161 Kappa : 0.6325
Mcnemar's Test P-Value : 0.02227 Mcnemar's Test P-Value : 0.04114
Sensitivity : 0.8926 Sensitivity : 0.8926
Specificity : 0.7130 Specificity : 0.7304
Pos Pred Value : 0.8012 Pos Pred Value : 0.8110
Neg Pred Value : 0.8367 Neg Pred Value : 0.8400
Prevalence : 0.5644 Prevalence : 0.5644
Detection Rate : 0.5038 Detection Rate : 0.5038
Detection Prevalence : 0.6288 Detection Prevalence : 0.6212
'Positive' Class : Active 'Positive' Class : Active 45
50. 考察
精度の比較
決定木 Bagging Boosting RF
0.7424 0.803 0.8144 0.822
46
51. 考察
精度の比較
決定木 Bagging Boosting RF
0.7424 0.803 0.8144 0.822
ランダムフォレストが高い!
46
53. 参考文献
• 上田修功. アンサンブル学習の新展開. 電子情報通信学会技術研究報告. PRMU, パターン認識・
メディア理解, Vol. 102, No. 379, pp. 31‒36, 2002.
• Y. Freund, R.E. Schapire, 阿部直樹. ブースティング入門. 人工知能学会誌, Vol. 14, pp.
771‒780, 1999.
• Random forests - classification description
• T. Hastie, R. Tibshirani, J. Friedman. The Elements of Statistical Learning
--- Data Mining, Inference, and Prediction Second Edition. Springer, 2009.
• 金森 敬文, 竹之内 高志, 村田 昇. パターン認識. Rで学ぶデータサイエンス (Vol. 5). 共立出版,
2009.
48
Editor's Notes \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n