【发布时间】:2019-07-17 17:12:02
【问题描述】:
我正在使用这样的代码:
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))
将模型作为字符串写入数据帧。打印太多了...
【问题讨论】:
-
模型没有运行。好吧,它确实有错误,你能提供一个可重现的例子吗?!
-
我正在使用虹膜套装 - 非常可重现恕我直言...
-
请运行它,看看它会产生什么。先去掉警告部分。
-
很抱歉,我刚刚做了,它工作正常 - 您的错误信息是什么?
-
我很确定有你的名声的人使用 iris 得到一个简单的回归树来工作?好吧,它对我有用,对不起......