r - 获取 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))
将模型作为字符串写入数据帧。打印太多了...
解决方案
不完全确定预期的输出,但我们可以将模型保存为字符,如下所示:
model <- quote(fit_data$finalModel)
然后,我们可以在需要时简单地调用它,如下所示:
eval(model)
推荐阅读
- python - NumPy 就地重塑数组
- flutter - 如何在flutter中从sqlite获取最后一个条目?
- laravel - 如何在 laravel 中的 DB select 和 DB raw 之后获取值
- android - 在 Repository 类中观察 Forever 是一个好习惯吗?db+network 分页列表
- javascript - 从 iframe 中触发子 iframe 的关闭事件,但不存储/传递 iframe 元素 ID?
- python - 使用来自 API 的值的异步更新接口
- eclipse - Eclipse 市场 - 无法安装 asciidoc 编辑器
- c++ - 如何在 C++ 中运行具有仅调用线程的函数的类的多个对象?
- visual-studio-code - 如何在更漂亮(或 es-lint)中添加自定义格式
- ios - 删除TableView的底行