Jot*_* eN 21 plot r rpart r-caret
我正在使用插入包来使用rpart包对数据建模.
library('caret')
data(iris)
formula <- as.formula(Species ~.)
t <- train(formula,iris,method = "rpart",cp=0.002,maxdepth=8)
plot(t)
Run Code Online (Sandbox Code Playgroud)
结果我得到了对象't',我试图绘制这个对象来获得树图.但结果看起来像这样:

有没有办法从插入火车对象制作树图?
Jot*_* eN 42
看起来更漂亮的树图:
library(rattle)
fancyRpartPlot(t$finalModel)
Run Code Online (Sandbox Code Playgroud)

And*_*rie 24
返回的对象caret::train()是一个列表.元素finalModel包含您的模型.
试试这个:
plot(t$finalModel)
text(t$finalModel)
Run Code Online (Sandbox Code Playgroud)

有同样的问题,但这里给出的答案不会解决它,因为我使用随机森林而不是树,以下是所有来到这里有相同的问题:
简而言之:当方法类似于以下情况时,只能显示树:
method = "rpart"
Run Code Online (Sandbox Code Playgroud)
使用随机森林
method = "rf"
Run Code Online (Sandbox Code Playgroud)
扩展答案已在此处: 在R(Caret)中绘制决策树