使用R的决策树

时间:2018-02-13 18:17:54

标签: r decision-tree

鉴于此数据:

       Siblings Children Gender Survival
1         Y        Y      F        Y
2         N        Y      M        N
3         Y        Y      M        Y
4         N        N      F        N
5         Y        N      F        N
6         N        Y      F        N
7         Y        Y      M        N
8         Y        Y      M        Y
9         Y        N      F        Y
10        Y        Y      F        N
11        Y        N      M        N

当我使用该功能时:

fit <- rpart(Survival ~ Gender + Children + Siblings,
             data = data1, method = "class")
plot(fit)

有一条错误消息说:

  

fit不是树,只是一个根。

我如何拟合和绘制决策树?

1 个答案:

答案 0 :(得分:1)

当我输入您的密码时,它也会显示相同的消息。我检查了里面的样子:

> fit
n= 11 

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

1) root 11 4 N (0.6363636 0.3636364) *

似乎这些数据不足以使R创建有意义的东西。

fancyRpartPlot包中的rattle函数在较小的数据集上效果更好:(取自答案:Decision trees in smaller datasets):

library(rattle)
fit <- rpart(Survival ~ Gender + Children + Siblings,
             data = data, method = "class",
             control = rpart.control(minbucket=2))
fancyRpartPlot(fit, sub=NULL)

结果如下:

Decision tree

相关问题