手动修剪决策树

问题描述 投票:0回答:1

我使用rpart()函数生成了一个简单的树,但是我希望能够在Petal.Length < 4.9分割之前停止Petal.Width的第二次分割,但是我不想改变树中的其他任何东西。我发现的唯一的事情是我可以使用子集函数来手动增长树,但这个过程可能非常繁琐。有关可能使用的功能的任何建议吗?用于生成树的代码是:

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)

Decision Tree

r machine-learning decision-tree rpart pruning
1个回答
0
投票

一种方法是使用sniprpart.plot参数:

trimmed.tree <- rpart.plot(m1, snip=TRUE))$obj   # manually trim the tree
rpart.plot(trimmed.tree)                         # display the trimmed tree

这会将树放在屏幕上,您可以使用鼠标手动修剪树。有关详细信息,请参阅rpart.plot包vignette http://www.milbo.org/doc/prp.pdf的第9章“使用鼠标修剪树”。

© www.soinside.com 2019 - 2024. All rights reserved.