Your SlideShare is downloading. ×
20150329 tokyo r47
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

20150329 tokyo r47

0
views

Published on

レベル2を目指す人のためのランダムフォレストまとめ

レベル2を目指す人のためのランダムフォレストまとめ


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
0
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. レベル2を⽬目指す⼈人のための   ランダムフォレスト   まとめ ! Tokyo.R#47   2015-‐‑‒03-‐‑‒28   @kashitan
  • 2. >  summary(kashitan) • TwitterID  :  @kashitan   • お仕事  :  某通信会社 2
  • 3. 3/13  データサイエンティストのスキル 要件について盛り上がる 3
  • 4. 似たようなのは以前からあった 4
  • 5. 第43回  R勉強会@東京でも紹介されて   いる Tokyo.R#43 LT 「32bit Windowsで頑張るRandom Forest」@fqz7c3
  • 6. ランダムフォレストって情報たくさん   あるけどまとまってなくね?
  • 7. ということでまとめてみた 1. ランダムフォレストおさらい 2. 不均衡データへの対応 3. パラメータチューニング 4. 重要な説明変数の確認 5. 処理の並列化 7
  • 8. 1.  ランダムフォレストおさらい
  • 9. ランダムフォレスト?   なにそれ?   おいしいの? 9
  • 10. Wikipediaより(1/2) 10
  • 11. Wikipediaより(2/2) 11
  • 12. https://citizennet.com/blog/2012/11/10/random-forests-ensembles-and-performance-metrics/
  • 13. Tokyo.R#21 LT 「アンサンブル学習」@holidayworking 使い⽅方 13
  • 14. 実⾏行行例例 > library(randomForest) > mdl <- randomForest(AGI~.-INSTWGHT, data=d.t) > print(mdl) ! Call: randomForest(formula = AGI ~ . - INSTWGHT, data = d.t) Type of random forest: classification Number of trees: 500 No. of variables tried at each split: 6 ! OOB estimate of error rate: 6.2% Confusion matrix: - 50000. 50000+. class.error - 50000. 187117 23 0.0001229026 50000+. 12353 29 0.9976578905 14
  • 15. 2.  不不均衡データへの対応
  • 16. Tokyo.R#20「不均衡データのクラス分類」@sfchaos 不不均衡データって? 16
  • 17. Tokyo.R#20「不均衡データのクラス分類」@sfchaos ランダムフォレストでの対応⽅方法 17
  • 18. Rでのウェイト指定
  • 19. Rでのウェイト指定 • randomForest(…, classwt=c(1, n))で
 指定 ! ! ! 19
  • 20. 実⾏行行例例 > system.time(mdl.wt <- randomForest(AGI~.-INSTWGHT, data=d.t, classwt=c(1,15))) ユーザ システム 経過 774.959 16.124 796.331 > print(mdl.wt) ! Call: randomForest(formula = AGI ~ . - INSTWGHT, data = d.t, classwt = c(1, 15)) Type of random forest: classification Number of trees: 500 No. of variables tried at each split: 6 ! OOB estimate of error rate: 6.21% Confusion matrix: - 50000. 50000+. class.error - 50000. 187140 0 0 50000+. 12382 0 1
  • 21. ウェイトの
 指定方法
 ご教示ください
  • 22. 3.  パラメータチューニング
  • 23. チューニングが必要なパラメータ http://d.hatena.ne.jp/shakezo/20121221/1356089207
  • 24. Rでのチューニング⽅方法
  • 25. チューニングパラメータ • ntree(木の数) • モデル作成後に決定 ! • mtry(1つの木で使用する特徴量の数) • tuneRF()で決定 25
  • 26. tuneRF()  実⾏行行例例 > system.time(mdl.tune <- tuneRF(x=d.t[, -42], y=d.t[, 42], doBest=T)) mtry = 6 OOB error = 6.14% Searching left ... mtry = 3 OOB error = 6.18% -0.006857703 0.05 Searching right ... mtry = 12 OOB error = 6.17% -0.004326884 0.05 ユーザ システム 経過 1002.426 14.769 1022.432 26
  • 27. 実⾏行行例例 ↑   特徴量量の数(mtry)は6で良良さそう 27
  • 28. 実⾏行行例例 > plot(mdl) ↑   ⽊木の数(ntree)は100でも⼗十分そう 28
  • 29. チューニング結果 > mdl.tuned <- randomForest(AGI~.-INSTWGHT, data=d.t, ntree=100, mtry=6) > print(mdl.tuned) ! Call: randomForest(formula = AGI ~ . - INSTWGHT, data = d.t, ntree = 100, mtry = 6) Type of random forest: classification Number of trees: 100 No. of variables tried at each split: 6 ! OOB estimate of error rate: 6.18% Confusion matrix: - 50000. 50000+. class.error - 50000. 187090 50 0.0002671797 50000+. 12271 111 0.9910353739
  • 30. 4.  重要な説明変数の確認
  • 31. で結局収入に影響する
 変数は何?
  • 32. 重要な説明変数
  • 33. 重要な説明変数の確認⽅方法 • 全変数の重要度 • importance() • varImpPlot() ! • 各変数の寄与 • patialPlot() 33
  • 34. importance()  実⾏行行例例 > importance(mdl.tuned) MeanDecreaseGini AAGE 1705.651869 ACLSWKR 544.340658 ADTIND 1649.357768 ADTOCC 2332.457474 AHGA 1823.620156 AHRSPAY 228.468096 AHSCOL 8.161362 AMARITL 340.210957 AMJIND 915.882423 AMJOCC 1216.616396 ARACE 175.041013 AREORGN 148.378241 ASEX 575.004856 AUNMEM 230.627948
  • 35. varImpPlot()  実⾏行行例例 > varImpPlot(mdl.tuned)
  • 36. patialPlot()  実⾏行行例例 > partialPlot(mdl.tuned, d.t, ADTOCC," 50000+.") ↑   職業コード37は収⼊入が多そう
  • 37. patialPlot()  実⾏行行例例 > partialPlot(mdl.tuned, d.t, AAGE," 50000+.") ↑   年年齢が⾼高くなると収⼊入が多くなりそう
  • 38. 5.  処理理の並列列化
  • 39. https://citizennet.com/blog/2012/11/10/random-forests-ensembles-and-performance-metrics/ Rでも並列で処理できる
  • 40. 並列列処理理の仕⽅方 ! 著者 :福島 真太朗 出版社 :ソシム 価格 :3,888円 ! P.144にランダムフォレストの 並列化について記載 40
  • 41. 並列列処理理の仕⽅方
  • 42. 並列列処理理結果 > library(foreach) > library(doMC) > registerDoMC(4) > system.time( + mdl.p <- foreach(ntree = rep(25, 4), .combine = combine, export = "d.t", .packages = "randomForest") %dopar% { + randomForest(AGI~.-INSTWGHT, data = d.t, ntree = ntree, mtry=6) + } + ) ユーザ システム 経過 245.641 4.701 64.763 半分以下になった!42
  • 43. まとめ 1. ランダムフォレストおさらい 2. 不均衡データへの対応 3. パラメータチューニング 4. 重要な説明変数の確認 5. 処理の並列化 43