在 R 中绘制决策树(插入符号)

Plot decision tree in R (Caret)

我用 rf 方法训练了一个数据集。例如:

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
              )

在那之后,我想绘制决策树,但是当我写 plot(model) 时,我得到这个:plot(model).

如果我写 plot(model$finalModel),我会得到这个:plot(model$finalModel)

我想绘制决策树...

我该怎么做? 谢谢:)

您使用的模型是随机森林,它不是单个决策树,而是大量树的集合。绘制最终模型将随着树数的增加绘制训练和测试数据集上的错误率,如下所示。

如果您想要一个单一的决策树,您可能喜欢像下面这样训练 CART 模型:

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

现在如上绘制最终模型将为您绘制决策树。