首页 > 解决方案 > 获取 rpart 回归树的字符串/字符表示

问题描述

我正在使用这样的代码:

library(datasets)
library(rpart)
library(caret)

options(warn=-1)
set.seed(42)


x <- subset(iris, select=-c(Species, Sepal.Length))

fitControl <- trainControl(
     method = "repeatedcv"
     , number = 10
     , repeats = 10
 )

fit_data <- caret::train(
     x = x
     , y = iris$Sepal.Length
     , method = 'rpart'
     , trControl = fitControl
     #, control=rpart.control(minsplit=3, minbucket=1, cp=0.001)
     #, metric = "ROC"
     #, tuneLength = 20
    , control = rpart.control(maxdepth = 3) #  minbucket=20
)

model <- fit_data$finalModel
model

最后一行:

model

在屏幕上将模型打印为字符串/字符:

n= 150 

node), split, n, deviance, yval
      * denotes terminal node

1) root 150 102.1683000 5.843333  
  2) Petal.Length< 4.25 73  13.1391800 5.179452 *
  3) Petal.Length>=4.25 77  26.3527300 6.472727  
    6) Petal.Length< 6.05 68  13.4923500 6.326471 *
    7) Petal.Length>=6.05 9   0.4155556 7.577778 *

有没有办法明确地获取实际的字符串/字符表示?我试过这样的事情:

 df <- data.frame(test = as.character(model))

将模型作为字符串写入数据帧。打印太多了...

标签: rr-caretrpart

解决方案


不完全确定预期的输出,但我们可以将模型保存为字符,如下所示:

model <- quote(fit_data$finalModel)

然后,我们可以在需要时简单地调用它,如下所示:

eval(model)

推荐阅读