R中的决策树错误:fit不是树,只是一个根

Emi*_*lia 6 decision-tree

下午好!我对决策树有问题.

f11<-as.factor(Z24train$f1)
fit_f1 <- rpart(f11~TSU+TSL+TW+TP,data = Z24train,method="class")
plot(fit_f1, uniform=TRUE, main="Classification Tree for Kyphosis")
Run Code Online (Sandbox Code Playgroud)

但是出现此错误:

Error in plot.rpart(fit_f1, uniform = TRUE, main = "Classification Tree for Kyphosis") : 
  fit is not a tree, just a root
Run Code Online (Sandbox Code Playgroud)

这是什么问题?谢谢您的帮助 :)

小智 7

这可能是由于RPART在使用默认控制参数后无法使用给定数据集创建决策树.

rpart.control(minsplit = 20, minbucket = round(minsplit/3), cp = 0.01, 
maxcompete = 4, maxsurrogate = 5, usesurrogate = 2, xval = 10,
surrogatestyle = 0, maxdepth = 30, ...)
Run Code Online (Sandbox Code Playgroud)

如果要创建树,可以调整控制参数并创建过拟合树.

tree <- rpart(f11~TSU+TSL+TW+TP,data = Z24train,method="class",control =rpart.control(minsplit =1,minbucket=1, cp=0))
Run Code Online (Sandbox Code Playgroud)

参数描述取自r文档(https://stat.ethz.ch/R-manual/R-devel/library/rpart/html/rpart.control.html)

minsplit
节点中必须存在的最小观察数,以便尝试拆分.

minbucket
任何终端节点中的最小观察数.如果仅指定了minbucket或minsplit中的一个,则代码将minsplit设置为minbucket*3或minbucket设置为minsplit/3(视情况而定).

cp
复杂度参数.不尝试任何不会降低整体缺乏适应度的分裂.例如,通过anova分裂,这意味着整个R平方必须在每一步增加cp.此参数的主要作用是通过修剪明显不值得的拆分来节省计算时间.从本质上讲,用户通知程序任何不能通过cp改善拟合的分割都可能会被交叉验证修剪掉,因此程序无需追求它