Re: [問題] 隨機森林畫決策樹

作者: celestialgod (天)   2015-07-26 14:48:56
※ 引述《SFMAndroid (安卓發送)》之銘言:
: 承前篇
: 目前成功用plot.getTree得到結果了
: #code
: library(tree)
: library(reprtree)
: library(plotrix)
: set.seed(100)
: jepg("~//tree.jpeg", width=1300, height=800)
: reprtree:::plot.getTree(rf, k=10, depth=0)
: dev.off()
我看了一下,原作者好像寫死了那個部分的參數(cex = 0.8)
所以你需要用git clone把package download之後自己做一些修改
plor.reptree.R的 labelBG加上一個input, cex.label
boxed.labels裡面的cex引數原本等於0.8要改成 cex = cex.label
像下面這樣:
labelBG <- function(tr, cex.label = 0.8){
## 中間省略
boxed.labels(xy$x[ind],xy$y[ind]+0.5*charht, rows[ind] , border=F,
bg='white', cex=cex.label, xpad=0.5, ypad=1)
}
plot.getTree.R的plot.getTree加上一個input, cex.label
然後改倒數第三行的labelBG,改成labelBG(x, cex.label)
像這樣
plot.getTree <- function(rforest=NULL,tr=NULL,k=1, depth=0,main=NULL,
cex.label = 0.8, ...){
## 中間省略
labelBG(x, cex.label)
labelYN(x)
title(main=main)
}
你接下來這樣就可以work了
library(reprtree)
set.seed(100)
data <- data.frame(y=sample(c("好", "不好"), 100, replace=TRUE),
x1=sample(c("學生", "老師", "教官"), 100, replace=TRUE),
x2=sample(c("20歲以下", "20~30歲", "30歲以上"), 100, replace=TRUE))
rf <- randomForest(y~., data=data, ntree=10, proximity=TRUE)
reprtree:::plot.getTree(rf, k=10, depth=0, cex.label = 2)
作者: SFMAndroid (安卓發送)   2015-07-26 16:24:00
大大我用git載下來也修改了 可是跑的時候他還是給我Error:"cex.label" 不是一個繪圖參數我是把載下來的資料夾覆蓋原本R library裡面的reptree然後再改參數 改完後不知為何沒變@@輸入後他寫ERROR:dependencies 'randomForest','tree', 'plotrix' are not available for package'reprtree' *removing '~/reprtree'

Links booklink

Contact Us: admin [ a t ] ucptt.com