如何使用训练有素的插入符号对象来预测新数据(训练时未使用)?
How to use trained caret object to predict on new data (not used while training)?
我正在使用 caret
包在训练数据集上训练随机森林模型。我使用了 10 折交叉验证来得到一个对象说 randomForestFit
。现在我想用这个对象来预测新的数据集,比如 test_data
。我还想获得各自的 class 概率。我该怎么做?
我一直在使用 extractProb
函数,如下所示:
extractProb(randomForestFit, textX = test_data_predictors, testY = test_data_labels)
但它给了我意想不到的结果。
从 extractProb
帮助页面示例中,您需要将模型包装在列表中:
knnFit <- train(Species ~ ., data = iris, method = "knn",
trControl = trainControl(method = "cv"))
rdaFit <- train(Species ~ ., data = iris, method = "rda",
trControl = trainControl(method = "cv"))
predict(knnFit)
predict(knnFit, type = "prob")
bothModels <- list(knn = knnFit,
tree = rdaFit)
predict(bothModels)
extractPrediction(bothModels, testX = iris[1:10, -5])
extractProb(bothModels, testX = iris[1:10, -5])
所以以下应该有效:
extractProb(list(randomForestFit), textX = test_data_predictors, testY = test_data_labels)
编辑:
是的,将使用预处理。来自 documentation:
These processing steps would be applied during any predictions
generated using predict.train, extractPrediction or extractProbs (see
details later in this document). The pre-processing would not be
applied to predictions that directly use the object$finalModel object.
我正在使用 caret
包在训练数据集上训练随机森林模型。我使用了 10 折交叉验证来得到一个对象说 randomForestFit
。现在我想用这个对象来预测新的数据集,比如 test_data
。我还想获得各自的 class 概率。我该怎么做?
我一直在使用 extractProb
函数,如下所示:
extractProb(randomForestFit, textX = test_data_predictors, testY = test_data_labels)
但它给了我意想不到的结果。
从 extractProb
帮助页面示例中,您需要将模型包装在列表中:
knnFit <- train(Species ~ ., data = iris, method = "knn",
trControl = trainControl(method = "cv"))
rdaFit <- train(Species ~ ., data = iris, method = "rda",
trControl = trainControl(method = "cv"))
predict(knnFit)
predict(knnFit, type = "prob")
bothModels <- list(knn = knnFit,
tree = rdaFit)
predict(bothModels)
extractPrediction(bothModels, testX = iris[1:10, -5])
extractProb(bothModels, testX = iris[1:10, -5])
所以以下应该有效:
extractProb(list(randomForestFit), textX = test_data_predictors, testY = test_data_labels)
编辑:
是的,将使用预处理。来自 documentation:
These processing steps would be applied during any predictions generated using predict.train, extractPrediction or extractProbs (see details later in this document). The pre-processing would not be applied to predictions that directly use the object$finalModel object.