【问题标题】:Parallelise rfcv from the randomForest package in R从 R 中的 randomForest 包中并行化 rfcv
【发布时间】:2016-03-14 16:29:07
【问题描述】:

我正在尝试使用 rfcv 函数进行多元随机森林特征选择。我设法让正常的 rf 命令(构建随机森林)模型使用以下方法进行并行处理:

library(randomForest)
library(doMC)
nCores <- detectCores();
registerDoMC(nCores) #number of cores on the machine
rf.model <- foreach(ntree=rep(round(510/nCores),nCores), .combine=combine, .multicombine=TRUE, .packages="randomForest") %dopar% {
    rf <- randomForest(y = outcome, x = predictor, ntree=ntree, mtry=4,      norm.votes=FALSE, importance=TRUE)
  }

在使用这个之前,我想使用 rfcv 进行我的功能选择。我尝试使用以下方法按上述方式进行操作:

  rf.model <- foreach(1:nCores, .packages="randomForest") %dopar% {
    rf.rfcv <- rfcv(ytrain = outcome, xtrain = predictor, scale=4)
  }

但是,这个函数的结果多次重复,所以我只得到 rf.rfcv 作为 4 个相同结果的列表。

任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: r random-forest feature-selection


    【解决方案1】:

    randomForest 可以无缝并行运行,因为 randomForest::combine 函数会将 4 个 rf.objects 减少为一个对象。因此,在第一个代码示例中,您仅训练 4 个森林模型,其中随机种子不同。使用 combine=combine (implicit combine=randomForest::combine),您可以指定 4 个模型的输出列表应使用 randomForest 包中的专用 combine 函数进行缩减。

    rfcv 没有任何组合功能,简单地组合四个输出也没有任何意义。在您的代码中,foreach 只需运行该函数 4 次并在列表中返回输出。如果您想并行运行 rfcv,则可以解决以下问题:

    my.rfcv = randomForest::rfcv #copy function from package to .Global.env
    fix(my.rfcv) #inspect function and perhaps copy entire function to your source functions script
    
    #rewrite for-loop at line 35-57 into a foreach-loop
    #write a reducer to combine test results of each fold
    

    【讨论】:

      猜你喜欢
      • 2012-09-29
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2019-09-07
      • 2012-04-24
      • 2013-06-29
      • 2020-07-22
      相关资源
      最近更新 更多