4

您好,我在 R中并行化cforest时遇到问题。

我一直在尝试使用派对包的cforest函数创建分类模型。我希望它在我的计算机上的多个内核中并行运行。我已经使用randomForest算法结合.combineforeach成功地做到了这一点:

    library(doParallel)
    library(foreach)
    library(randomForest)
    library(party)        
    cl <- (5)
    registerDoParallel(cl)        
    set.seed(1234)
    abs_pos_dat_all <- read.csv('all_abs_pos_predictors_3_20_13_for_R.txt');
    abs_pos_dat <- abs_pos_dat_all[1:10000,]
    train_test_indices <- sample(2,nrow(abs_pos_dat), replace=TRUE, prob=c(.7,.3));
    ref_polarity_dat <- read.table('ref_polarity_3_20_13_for_R.txt');
    ref_polarity_dat <- factor(ref_polarity_dat[1:10000,])
    ref_polarity_train <- ref_polarity_dat[train_test_indices==1]
abs_pos_train[,1] <- ordered(abs_pos_train[,1], labels = c("Buried","Part buried","Exposed"))
abs_pos_train[,2] <- ordered(abs_pos_train[,2], labels = c("Helix","Strand","Other"))
Flank_FA_labels <- c("bur bur","bur part","part part","bur exp","part exp", "exp exp")
Flank_Struc_labels <- c("helix helix","helix strand","strand strand","helix other","strand other", "other other")
Flank_Polarity_labels <- c("polar polar", "polar nonpolar", "non polar non polar" )

    for(i in 1:length(Flank_FA_labels)){
        abs_pos_train[,i] <- ordered(abs_pos_train[,2+i], labels = Flank_FA_labels) 
        abs_pos_train[,8+i] <- ordered(abs_pos_train[,8+i], labels = Flank_Polarity_labels)
        abs_pos_train[,14+i] <- ordered(abs_pos_train[,14+i], labels = Flank_Struc_labels)
    }

   colnames(abs_pos_train) <- c("ref_FA", "ref_struc", "Np1Flank_FA", "Np2Flank_FA", "Np3Flank_FA", "Np4Flank_FA", "Np5Flank_FA", "Np6Flank_FA", "Np1Flank_Struc", "Np2Flank_Struc", "Np3Flank_Struc", "Np4Flank_Struc", "Np5Flank_Struc", "Np6Flank_Struc", "Np1Flank_P_NP","Np2Flank_P_NP", "Np3Flank_P_NP", "Np4Flank_P_NP", "Np5Flank_P_NP", "Np6Flank_P_NP")


    abs_pos_random_forest <<- foreach(ntree=rep(100, 5), .combine=combine, .packages='randomForest') %dopar%  randomForest(ref_polarity_train~.,data = abs_pos_train, ntree=ntree)

但是,当我使用与cforest相同的语法时,我收到以下错误:

    abs_pos_inference_random_forest <<- foreach(ntree=rep(20, 6), 
    .combine=combine , .packages='party') %dopar%  cforest(ref_polarity_train~.,
    data = abs_pos_train, controls = cforest_unbiased(ntree=ntree, mtry = 1))
    error calling combine function:
    <simpleError in fun(result.1, result.2): 
    Argument must be a list of randomForest objects>

我无法弄清楚为什么 .combine 正在寻找randomForest对象而不是cforest对象,或者至少为什么 .combine 不会自动识别正在组合的对象的类型,如果它们都是单个函数的输出。

4

1 回答 1

1

您将收到相同的错误消息执行:

library(randomForest)
combine(1,2,3)

大概是加载了randomForest包,所以combine通过选项将randomForest函数传递给了foreach .combine。如果combine是具有由 randomForest 和 party 定义的方法的通用函数,那么它可能会按您的预期工作。但这不是通用的。它只是 randomForest 包中定义的常规函数​​,foreach 尽职尽责地调用了它。

我对party包不是很熟悉,所以不知道它是否包含等效功能。

于 2013-03-31T19:29:51.333 回答