rpart 在根节点处停止,当有明显的信息增益时不再进一步分裂

rpart stops at root node and does not split further when there is an obvious information gain

我正在尝试使用 rpart 构建分类树模型。 测试数据框非常简单,仅包含 10 行中的两个布尔变量。 隐藏逻辑也很简单:当 x 为 FALSE 时,y 必须为 FALSE。当 x 为真时,y 有 60% 的概率为真。 所以我想 rpart 会在 x 上进行一次拆分以增加节点纯度。但它停留在根节点,根本不分裂。有人请指教吗?

> df <- data.frame(x=rep(c(FALSE,TRUE), each=5), y=c(rep(FALSE,7), rep(TRUE,3)))
> df
       x     y
1  FALSE FALSE
2  FALSE FALSE
3  FALSE FALSE
4  FALSE FALSE
5  FALSE FALSE
6   TRUE FALSE
7   TRUE FALSE
8   TRUE  TRUE
9   TRUE  TRUE
10  TRUE  TRUE
> rpart(y~x, method='class', data=df)
n= 10 

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

1) root 10 3 FALSE (0.7000000 0.3000000) *

正如我在评论中所说,这是为了避免过度拟合。形式上,有参数 minsplit,它预设为 20,但可以调整以给出您寻求的结果:

> library(rpart)
> df <- data.frame(x=rep(c(FALSE,TRUE), each=5), y=c(rep(FALSE,7), rep(TRUE,3)))
> rpart(y ~ x, data=df, minsplit=2)
n= 10 

node), split, n, deviance, yval
      * denotes terminal node

1) root 10 2.1 0.3  
  2) x< 0.5 5 0.0 0.0 *
  3) x>=0.5 5 1.2 0.6 *

中找到更多关于避免过度拟合的论据(即 cpmaxdepth
help(rpart.control)

编辑:使用方法="class",输出变为

> rpart(y ~ x, data=df, minsplit=2, method="class")
n= 10 

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

 1) root 10 3 FALSE (0.7000000 0.3000000)  
  2) x< 0.5 5 0 FALSE (1.0000000 0.0000000) *
  3) x>=0.5 5 2 TRUE (0.4000000 0.6000000) *