简体   繁体   English

使用 R 的决策树

[英]Decision Tree using R

Given this data:鉴于此数据:

       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

When I use the function:当我使用该功能时:

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

There is an error message that says:有一条错误消息说:

fit is not a tree, just a root.适合不是一棵树,只是一个根。

How can I fit and plot a decision tree?如何拟合和绘制决策树?

When I entered your code, it also displayed the same message.当我输入您的代码时,它也显示了相同的消息。 I checked what it looks like inside:我检查了里面的样子:

> fit
n= 11 

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

1) root 11 4 N (0.6363636 0.3636364) *

It seems that this data is simply not enough for R to create something meaningful.似乎这些数据根本不足以让 R 创造出有意义的东西。

The fancyRpartPlot function from the rattle package works better with smaller data sets: (Taken from this answer: Decision trees in smaller datasets ):来自rattle包的fancyRpartPlot函数在较小的数据集上效果更好:(取自这个答案: 较小数据集中的决策树):

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

And the result is as follows:结果如下:

决策树

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM