[英]Manually Pruning a Decision Tree
I have generated a simple tree using the rpart()
function, however I would like to be able to stop the second split at Petal.Length < 4.9
before it splits by Petal.Width
, however I would not like to alter anything else in the tree. 我已经使用rpart()
函数生成了一个简单的树,但是我希望能够在Petal.Length < 4.9
之前停止第二个拆分,然后再将其拆分为Petal.Width
,但是我不想更改树。 The only thing that I have found is that I can use the subset function in order to manually grow the tree, but this process can be very tedious. 我发现的唯一一件事是,我可以使用子集功能来手动生长树,但是此过程可能非常繁琐。 Any suggestions on a function that could possibly be used? 关于可能使用的功能的任何建议? The code used to generate the tree is: 用于生成树的代码是:
library(rpart)
library(datasets)
data("iris")
library(rpart.plot)
Sample <-sample.int(n = nrow(iris), size = floor(.7*nrow(iris)), replace = F)
train <- iris[Sample, ]
test <- iris[-Sample, ]
m1 <- rpart(Species~Sepal.Width + Sepal.Length + Petal.Length + Petal.Width,
data = train, control = rpart.control(cp = 0.005), method = "anova")
rpart.plot(m1, type = 3, fallen.leaves = TRUE)
One approach is to use the snip
argument of rpart.plot
: 一种方法是使用rpart.plot
的snip
参数:
trimmed.tree <- rpart.plot(m1, snip=TRUE))$obj # manually trim the tree
rpart.plot(trimmed.tree) # display the trimmed tree
This puts the tree on the screen, which you can manually prune with the mouse. 这会将树放在屏幕上,您可以使用鼠标手动修剪它。 For details, see Chapter 9 "Trimming a tree with the mouse" of the rpart.plot package vignette http://www.milbo.org/doc/prp.pdf . 有关详细信息,请参见rpart.plot程序包插图的第9章“用鼠标修剪树” http://www.milbo.org/doc/prp.pdf 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.