【问题标题】:Scoring a random forest model created from parallel processing对通过并行处理创建的随机森林模型进行评分
【发布时间】:2018-03-12 11:36:36
【问题描述】:

我正在尝试使用 foreach 包运行随机森林以进行并行处理。这是我正在运行的代码。

library(doParallel)
library(doMC)
library(foreach)
library(randomForest)

Train <- read.csv("Train_Parallel.csv")
Test <- read.csv("Test_Parallel.csv")
Scoring <- read.csv("Scoring_Parallel.csv")


cores = detectCores()-1
cl = makeCluster(cores)
registerDoParallel(cl)

startparallel <- Sys.time()
rf_parallel <- foreach(ntree=rep(400, cores), .combine=combine, .multicombine=TRUE,
              .packages='randomForest') %dopar% {
                randomForest(target ~ .,
                             data=Train,
                             importance=TRUE,
                             ntree=ntree,
                             mtry = 25)
              }
endparallel <- Sys.time()
stopCluster(cl)
endparallel - startparallel

并行执行代码按预期运行。但是,当我对我的训练和测试数据集运行预测函数时,出现以下错误。我做错了什么?

> Train$Predicted <- predict(rf_parallel, Train)
Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "list"
> Test$Predicted <- predict(rf_parallel, Test)
Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "list" 

【问题讨论】:

  • foreach 将为每个核心返回一个随机森林模型。每个核心的每个随机森林模型都作为列表的元素返回给rf_parallel。这意味着rf_parallel 是一个列表(长度(num_cores)),列表中的每个元素都是一个随机森林模型。对于predict(model, data),它需要一个模型,而不是模型列表。例如,尝试predict(rf_parallel[[1]], Train),我猜这将毫无错误地工作。我的猜测是你误解了你到底在并行化什么。
  • 感谢您的回复。因为,我有 4 个核心,所以构建了 3 个(detectCores()-1)随机森林模型,每个模型的 ntree=400。 “.combine=combine”不应该将列表组合成“random.Forest”类吗?
  • 没有。输入str(rf_parallel),查看rf_parallel 是什么。 (顺便说一句,如果可能的话,我不确定如何合并模型。你可以看看 stackoverflow 社区是否有任何想法。)
  • 但是,如果我不能使用并行处理来对我的数据集进行评分,那么并行处理的意义何在?
  • 这是一个很好的问题...

标签: r random-forest predict parallel-foreach


【解决方案1】:

确保 randomForest::combine 没有被 dplyr::combine 或其他东西掩盖

【讨论】:

    猜你喜欢
    • 2014-04-30
    • 2016-09-09
    • 1970-01-01
    • 2019-05-04
    • 2015-12-15
    • 2018-05-18
    • 2012-12-15
    • 2021-10-03
    • 2017-12-12
    相关资源
    最近更新 更多