parallelパッケージを使ってRandomForestを並列化

通常と並列化の比較

通常のランダムフォレスト

library(randomForest)
library(kernlab)

data(spam)
set.seed(777)
system.time(fit.rf<-randomForest(type~.,data=spam,ntree=1000))
ユーザ システム 経過
21.584 0.213 21.919

並列化のランダムフォレスト

library(parallel)
cores<-detectCores()
cl<-makeCluster(cores,type="PSOCK")
invisible(clusterEvalQ(
    cl,{
        library(randomForest)
        library(kernlab)
        data(spam)
    }))
set.seed(123)
clusterSetRNGStream(cl)
print(system.time(res.pa<-parLapply(cl,rep(250,cores),function(ntree) randomForest(type~.,data=spam,ntree=ntree))))
ユーザ システム 経過
0.166 0.166 10.934

並列化方法解説

Rのsnowパッケージを使ってrandomForestを並列化

cores<-detectCores()
cl<-makeCluster(cores,type="PSOCK")

参考資料