简体   繁体   English

在R(Caret)中绘制决策树

[英]Plot decision tree in R (Caret)

I have trained a dataset with rf method. 我已经用rf方法训练了一个数据集。 For example: 例如:

ctrl <- trainControl(
                     method = "LGOCV", 
                     repeats = 3, 
                     savePred=TRUE,
                     verboseIter = TRUE,
                     preProcOptions = list(thresh = 0.95)
                    )

preProcessInTrain<-c("center", "scale")
metric_used<-"Accuracy"
model <- train(
               Output ~ ., data = training,
               method = "rf",
               trControl = ctrl,
               metric=metric_used,
               tuneLength = 10,
               preProc = preProcessInTrain
              )

After thath, I want to plot the decission tree, but when I wirte plot(model) , I get this: plot(model) . 在那之后,我想绘制决策树,但是当我想出plot(model) ,我得到了: plot(model)

If I write plot(model$finalModel) , I get this : plot(model$finalModel) 如果我写plot(model$finalModel) ,我会得到这个: plot(model$finalModel)

I would like to plot the decission tree... 我想画出决定树...

How can I do that? 我怎样才能做到这一点? Thanks :) 谢谢 :)

The model you are using is random forest, which is not a single decision tree, but an ensemble of a large number of trees. 您使用的模型是随机森林,它不是单个决策树,而是大量树的集合。 Plotting the final model will plot the error rates on the training and test datasets as # of trees are increased, something like the following. 绘制最终模型将随着树木数量的增加在训练和测试数据集上绘制错误率,如下所示。

在此处输入图片说明

If you want a single decision tree instead, you may like to train a CART model like the following: 如果您只想要一个决策树,则可能需要像以下这样训练CART模型:

model <- train(
  Species ~ ., data = training,
  method = "rpart",
  trControl = ctrl,
  metric=metric_used,
  tuneLength = 10,
  preProc = preProcessInTrain
)
library(rpart.plot)
rpart.plot(model$finalModel)

Now plotting the final model as above will plot the decision tree for you. 现在如上所述绘制最终模型将为您绘制决策树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM