我想使用pmml库导出一个Caret随机森林模型,以便在Java中进行预测。以下是我遇到的错误复制内容。
data(iris)
require(caret)
require(pmml)
rfGrid2 <- expand.grid(.mtry = c(1,2))
fitControl2 <- trainControl(
method = "repeatedcv",
number = NUMBER_OF_CV,
repeats = REPEATES)
model.Test <- train(Species ~ .,
data = iris,
method ="rf",
trControl = fitControl2,
ntree = NUMBER_OF_TREES,
importance = TRUE,
tuneGrid = rfGrid2)
print(model.Test)
pmml(model.Test)
Error in UseMethod("pmml") :
no applicable method for 'pmml' applied to an object of class "c('train', 'train.formula')"
我搜索了一段时间,发现关于导出到PMML的信息很少。总的来说,pmml库中包含随机森林。
methods(pmml)
[1] pmml.ada pmml.coxph pmml.cv.glmnet pmml.glm pmml.hclust pmml.itemsets pmml.kmeans
[8] pmml.ksvm pmml.lm pmml.multinom pmml.naiveBayes pmml.nnet pmml.randomForest pmml.rfsrc
[15] pmml.rpart pmml.rules pmml.svm
这个功能使用一个直接的随机森林模型,但不是caret训练出来的。
library(randomForest)
iris.rf <- randomForest(Species ~ ., data=iris, ntree=20)
# Convert to pmml
pmml(iris.rf)
# this works!!!
str(iris.rf)
List of 19
$ call : language randomForest(formula = Species ~ ., data = iris, ntree = 20)
$ type : chr "classification"
$ predicted : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
...
str(model.Test)
List of 22
$ method : chr "rf"
$ modelInfo :List of 14
..$ label : chr "Random Forest"
..$ library : chr "randomForest"
..$ loop : NULL
..$ type : chr [1:2] "Classification" "Regression"
...
rf = randomForest(Species ~ ., data = iris)
。矩阵接口:rf = randomForest(y = iris[, c("Species")], x = iris[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")], data = iris)
。 - user1808924pmml
方法仅接受使用公式接口训练的 RF 模型。对于使用矩阵接口训练的 RF 模型,它会引发错误。不幸的是,Caret 包使用了矩阵接口。 - user1808924