将Randomforest树转换并表示为决策树(rpart)

时间:2017-06-04 18:20:16

标签: r random-forest decision-tree rpart

我有随机林的以下代码:

set.seed(71) 
rf = randomForest(Species~.,data=iris,ntree=200,mtry=2,sampsize=30,keep.forest=TRUE,replace=FALSE,keep.inbag=TRUE)

我想获得第200个决策树,所以我使用:

> getTree (rf,200)
  left daughter right daughter split var split point status prediction
1             2              3         3        2.50      1          0
2             0              0         0        0.00     -1          1
3             4              5         3        4.75      1          0
4             0              0         0        0.00     -1          2
5             6              7         3        5.05      1          0
6             8              9         1        6.45      1          0
7             0              0         0        0.00     -1          3
8             0              0         0        0.00     -1          3
9             0              0         0        0.00     -1          2

但我想将第200棵树用作决策树,例如rpart。所以它的格式如下:

> getTree (rf,200).rpart
n= 111 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 111 35 other (0.68468468 0.31531532)  
  2) Petal.Length< 4.95 77  3 other (0.96103896 0.03896104) *
  3) Petal.Length>=4.95 34  2 virginica (0.05882353 0.94117647) * 

有没有办法转换getTree (rf,200) dataframe

> str(getTree (rf,200))
 num [1:9, 1:6] 2 0 4 0 6 8 0 0 0 3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:9] "1" "2" "3" "4" ...
  ..$ : chr [1:6] "left daughter" "right daughter" "split var" "split point" ...

进入真正的rpart决策树对象?

0 个答案:

没有答案