[英]Read multiple rds models and use predict function in lapply?
I have bunch of models saved as .rds, after reading all models and I use the predict function in loop on test data, showing error on predict method, but using individual model is working. 在读取所有模型后,我将一堆模型保存为.rds,我在测试数据的循环中使用预测函数,显示预测方法的错误,但使用单个模型正在工作。 (model are built using mlr package) (模型使用mlr包构建)
I read all models from a folder and use lapply on the test data, 我从一个文件夹中读取所有模型并在测试数据上使用lapply,
files = list.files(path = 'C:/rf_models', pattern = '\\.rds$', full.names = TRUE)
read_models <- do.call("rbind", lapply(files, readRDS))
print(lapply(read_models, function (x) predict(x, newdata = as.data.frame(test_data))))
its showing below Error and opening a Browse[1]> in console 它显示在Error下面并在控制台中打开Browse [1]>
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "c('FilterWrapper', 'BaseWrapper', 'Learner')"
Called from: predict(x, newdata = as.data.frame(test_data))
Browse[1]>
Even if I use for loop 即使我用于循环
for (i in 1:80){
pred_models <- predict(read_models[[i]], newdata = as.data.frame(test_data))
}
also showing 也表明
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "c('FilterWrapper', 'BaseWrapper', 'Learner')"
All done in same R script where mlr library I already loaded 所有在我已加载mlr库的R脚本中完成
if I read model individually, it is working 如果我单独阅读模型,它是有效的
model <- readRDS("C:/rf_models/rf_models_31.rds")
prediction <- predict(model, newdata = as.data.frame(test_data))
prediction
Prediction: 1 observations
predict.type: prob
threshold: 0=0.50,1=0.50
time: 0.03
truth prob.0 prob.1 response
1 0 0.12 0.88 1
I expect the pred_models to have all the predictions stored. 我希望pred_models能够存储所有预测。
just loading using the file names it worked 只使用它工作的文件名加载
files = list.files(path = 'C:/rf_models', pattern = '.rds$', full.names = TRUE)
for (i in 1:80){
model <- readRDS(files[i])
prediction <- predict(model, newdata = as.data.frame(test_data))
print(prediction)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.