【问题标题】:R: randomForest error when combining forest produced using CaretR:组合使用插入符号生成的森林时出现随机森林错误
【发布时间】:2016-05-11 08:09:44
【问题描述】:

我正在尝试使用 randomForest 'combine' 函数在 R 中组合多个随机森林,但不能使用来自 'caret' 包包装器的 randomForest 输出。

返回的对象具有“train”类,而不是“randomForest”类 - 请问有什么想法吗?

我不清楚在运行插入符号的“train”函数后如何检索 randomForest 对象,我认为它应该包含它们。

原因是我正在对大型数据集进行分析,太大而无法在我的硬件上运行 randomForest。

为了使用可用内存管理数据集,我首先生成了许多较小的森林,然后使用 rf 'combine' 函数将它们组合起来。结果很好,我想对 caret 的输出做同样的事情。

问题代码的概述(我宁愿使用应用函数而不是循环,但我也不清楚应用到这个例子中)

trainData.Slices <- list() #My data is 'sliced' into manageable pieces, each one being run through randomForest individually before being recombined 
trainData.Slices[[1]] <-data.frame("y.val" = runif(1000, 0, 1), pred1 = runif(1000, 1, 5), pred1 = runif(1000, 10, 20))
trainData.Slices[[2]] <- data.frame("y.val" = runif(1000, 0, 1), pred1 = runif(1000, 1, 5), pred1 = runif(1000, 10, 20))
trainData.Slices[[3]] <- data.frame("y.val" = runif(1000, 0, 1), pred1 = runif(1000, 1, 5), pred1 = runif(1000, 10, 20))


slicesRun <- length(trainData.Slices) #Specify how many slices to cut the data into for individual processing
forestList <- list() #The list into which each small forest will be added
nVar <- length(trainData.Slices[[1]])


for (i in 1:slicesRun) {
trainData <- trainData.Slices[[i]]

#The standard randomForest code works perfectly
forestList[[i]] <- randomForest(x=trainData[,-1], y=trainData[,1],ntree=200, importance=TRUE, proximity=TRUE)
print(class(forestList[[i]])) 

#caret is returning 'train' objects rather than randomForest objects
forestList_caret[[i]] <- train(y=trainData[,1], x=trainData[,-1], method="rf", trControl=trainControl(method="cv", number=5), prox=TRUE, allowParallel=TRUE)
print(class(forestList_caret[[i]])) 
#How can the rf objects be returned instead, or train objects combined?

} 


rf.all <- do.call("combine",forestList) #Combine the forests into one
rf.all_caret <- do.call("combine",forestList) #Combine the forests into one    

【问题讨论】:

  • 欢迎来到 Stack Overflow - 请参阅 this FAQ 以获取有关提供可重现示例的提示。
  • 谢谢 nrussel。立即编辑。

标签: r random-forest r-caret


【解决方案1】:

我也遇到了这个问题,从这篇文章中发现了以下内容:Error when using predict() on a randomForest object trained with caret's train() using formula

randomForest 对象位于$finalModel 中,因此在您的示例中为forestList_caret[[i]]$finalModel。您的代码适用于以下更改:

第 8 行到forestList &lt;- forestList_caret &lt;- list()

第 28 行到rf.all_caret &lt;- do.call("combine",forestList_caret)

在第 22 行之后插入:

forestList_caret[[i]] &lt;- forestList_caret[[i]]$finalModel print(class(forestList_caret[[i]]))

存储$finalModel 对象让您可以在最后组合它们,结果是一个类randomForest 的对象。检查:

print(class(rf.all_caret))

【讨论】:

    猜你喜欢
    • 2016-08-10
    • 2017-01-22
    • 2015-10-08
    • 2012-06-03
    • 2016-04-10
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多