如何在插入包中制作树形图?

时间:2022-12-07 00:02:56

I'm using caret package to model the data using rpart package.

我正在使用插入包来使用rpart包对数据建模。

library('caret')
data(iris)
formula <- as.formula(Species ~.)
t <- train(formula,iris,method = "rpart",cp=0.002,maxdepth=8)
plot(t)

As a result I get object 't' and I'm trying to plot this object to get tree plot. But the result look like that: 如何在插入包中制作树形图?

结果我得到了对象't',我试图绘制这个对象来获得树图。但结果看起来像这样:

Are there any way to make a tree plot from caret train object?

有没有办法从插入符号列车对象制作树图?

3 个解决方案

#1


21  

The object returned from caret::train() is a list. The element finalModel contains your model.

从caret :: train()返回的对象是一个列表。元素finalModel包含您的模型。

Try this:

尝试这个:

plot(t$finalModel)
text(t$finalModel)

如何在插入包中制作树形图?

#2


33  

nicer looking treeplot:

看起来更漂亮的树图:

library(rattle)
fancyRpartPlot(t$finalModel)

如何在插入包中制作树形图?

#3


7  

Had the same problem, but the answers given here wouldn't solve it, since I used a random forest instead of a tree, the following is for all coming here having the same issue:

有同样的问题,但这里给出的答案不会解决它,因为我使用随机森林而不是树,以下是所有来到这里有相同的问题:

In short: A tree can only be displayed when the method is something like:

简而言之:只有当方法类似于以下内容时才能显示树:

method = "rpart"

Using a random forest

使用随机森林

method = "rf"

will result in the following plot: 如何在插入包中制作树形图?

将导致以下情节:

Extended answer already here: Plot decision tree in R (Caret)

扩展答案已在此处:在R(Caret)中绘制决策树

#1


21  

The object returned from caret::train() is a list. The element finalModel contains your model.

从caret :: train()返回的对象是一个列表。元素finalModel包含您的模型。

Try this:

尝试这个:

plot(t$finalModel)
text(t$finalModel)

如何在插入包中制作树形图?

#2


33  

nicer looking treeplot:

看起来更漂亮的树图:

library(rattle)
fancyRpartPlot(t$finalModel)

如何在插入包中制作树形图?

#3


7  

Had the same problem, but the answers given here wouldn't solve it, since I used a random forest instead of a tree, the following is for all coming here having the same issue:

有同样的问题,但这里给出的答案不会解决它,因为我使用随机森林而不是树,以下是所有来到这里有相同的问题:

In short: A tree can only be displayed when the method is something like:

简而言之:只有当方法类似于以下内容时才能显示树:

method = "rpart"

Using a random forest

使用随机森林

method = "rf"

will result in the following plot: 如何在插入包中制作树形图?

将导致以下情节:

Extended answer already here: Plot decision tree in R (Caret)

扩展答案已在此处:在R(Caret)中绘制决策树