【发布时间】:2015-02-13 15:34:11
【问题描述】:
我在使用插入符号 (R) 中的并行随机森林时遇到了一个问题。我看到它们是多个似乎处理同一个问题的问题,但在阅读完答案后,我仍然遇到同样的问题。
我有一个数据集,我用它来训练这样的模型:
rfParam <- expand.grid(mtry = 5)
parRFModel <- train(
form = Class~.,
data = datasetShorted,
method="parRF",
tuneGrid = rfParam
)
我可以使用这个模型进行预测,使用以下命令:
predictions <- extractPrediction(list(parRFModel), testX = datasetShorted[1:10,2:numFeatures])
然后我保存模型:
save(parRFModel, file="parRFModel-MTry5.RData")
问题是当我重新启动 R,重新加载所有库然后执行时
load("parRFModel-MTry5.RData")
模型已正确加载,但我无法预测:
> parRFModel
Parallel Random Forest
40794 samples
1947 predictors
8 classes: '0', '1', '2', '3', '4', '5', '6', '7'
No pre-processing
Resampling: Bootstrapped (25 reps)
Summary of sample sizes: 40794, 40794, 40794, 40794, 40794, 40794, ...
Resampling results
Accuracy Kappa Accuracy SD Kappa SD
0.6877108 0.477487 0.004078363 0.0072271
Tuning parameter 'mtry' was held constant at a value of 5
> predictions <- extractPrediction(list(parRFModel), testX = datasetShorted[1:10,2:numFeatures])
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "randomForest"
> class(parRFModel)
[1] "train" "train.formula"
您知道为什么会这样吗?保存/加载功能有问题吗?非常感谢!
【问题讨论】:
-
这是
caret和R的哪个版本?您是否也尝试过使用predict(parRFModel, datasetShorted[1:10,2:numFeatures])。看起来randomForest包没有加载。请给我们sessionInfo()崩溃后的结果。 -
你是英雄!原来我在调用 train 时间接加载了 randomForest ,但在第二种情况下它没有加载,这就是它崩溃的原因。首先调用 library(randomForest),解决了这个问题。非常感谢你!附言也可能不相关,这是最新版本 caret_6.0-37、R 版本 3.0.2 和 randomForest_4.6-10。
标签: r load random-forest predict r-caret