【问题标题】:Train a cforest in parallel并行训练一个 cforest
【发布时间】:2016-07-16 07:56:09
【问题描述】:

我有一个非常大的数据框,其中包含 790,000 行和 140 个预测变量。其中一些是相互密切相关的,并且在不同的尺度上。使用randomForest 包,我可以只使用一小部分数据样本在每个核心上种植森林,使用foreach 并将它们与combine() 函数合并以获得一棵大树,如下所示:

rf.STR = foreach(ntree=rep(125, 8), .combine=combine, .multicombine=TRUE, .packages='randomForest') %dopar% {
  sample.idx = sample.int( nrow(dat), size=sample.size, replace=TRUE)
  randomForest(x=dat[sample.idx,-1, with=FALSE], 
               y=dat[sample.idx, retention], ntree=ntree)
  }

不同尺度上的相关变量让我想使用party 包中的条件随机森林,但是cforests 没有combine() 方法,所以我不确定如何组合几个cforest 对象来获得一个重要性图或一个预测。

有没有办法在较小的数据子集上训练一个大的 cforest,或者制作几个小的 cforest 并将它们组合成一个更大的条件森林模型?

【问题讨论】:

  • 试试h2o可以下载的包here。非常快,开源并且并行运行。更新如此之快,以至于他们的文档有时缺乏一致性。
  • 感谢指点!从阅读 RF 文档来看,它似乎没有条件树。由于我的一些预测变量是高度相关的,如果我运行通常的 RF,变量重要性会将一堆高度预测但相关的变量放在顶部,这比只选择其中一个变量有用。
  • 实现party::cforest 不支持并行化(据我所知)。在(较慢的)重新实现partykit::cforest 中,我们添加了对并行化的支持,但目前partykit 版本尚未提供旧party 实现的所有功能。具体来说,目前没有实施可变重要性度量。因此,这对您没有多大用处。您可以直接联系party 维护者(Torsten Hothorn)是否有建议如何拆分party::cforest 的学习。
  • 谢谢,阿奇姆。这可能是我在这个主题上要做的最好的事情。如果您将其作为完整答案提交,我会接受它,直到partykit 获得importance() 函数:)。
  • 作为一个回答实在是太让人不满意了,一个评论就够尴尬了:-)

标签: r parallel-processing random-forest party


【解决方案1】:

制作几个小的 cforests 并将它们组合成一个更大的条件森林模型。

library(snowfall)
library(party)
cforestmt<-function(formula, data = list(), subset = NULL, weights = NULL, controls = cforest_unbiased(), xtrafo = ptrafo, ytrafo = ptrafo, scores = NULL, threads=8) {

    if(controls@ntree<threads) {    # if there are less trees than threads single thread
        return(cforest(formula, data = data, subset=subset, weights=weights, controls=controls, xtrafo=xtrafo, ytrafo=ytrafo, scores=scores))
    }

    # round off threads
    fsize=controls@ntree/threads
    if(fsize-round(fsize)!=0) {
            fsize=ceiling(fsize)
            message("Rounding forest size to ",fsize*threads)
    }
    controls@ntree=as.integer(fsize)

    # run forests in parallel
    sfInit(parallel=T, cpus=threads, type="SOCK")
    sfClusterEval(library(party))
    sfExport('formula','data','subset','weights','controls','xtrafo','ytrafo','scores')
    fr<-sfClusterEval(cforest(formula, data = data, subset=subset, weights=weights, controls=controls, xtrafo=xtrafo, ytrafo=ytrafo, scores=scores))
    sfStop()

    # combine/append forest
    fr[[1]]@ensemble<-unlist(lapply(fr,function(y) {y@ensemble}),recursive=F)
    fr[[1]]@where<-unlist(lapply(fr,function(y) {y@where}),recursive=F)
    fr[[1]]@weights<-unlist(lapply(fr,function(y) {y@weights}),recursive=F)

    #first forest has result
    return(fr[[1]])
}

【讨论】:

  • 您好,请在代码中添加一些解释,因为它有助于理解您的代码。仅代码的答案不受欢迎。
猜你喜欢
  • 1970-01-01
  • 2015-06-17
  • 2018-10-23
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多