using "foreach" for running different classifiers in R -
i trying use foreach run different classifiers on data, doesn't work. in fact doesn't return me anything.
purpose parallelize process. here simplified of code:
library(foreach) library(doparallel) no_cores <- detectcores() - 1 cl<-makecluster(no_cores) registerdoparallel(cl) registerdoparallel(no_cores) model_list<-foreach(i = 1:2, .combine = c,.packages=c("e1071","randomeforest")) %dopar% if (i==1){ model1<-svm(x = x,y = as.factor(y),type = "c-classification",probability = t) } if (i==2){ mode2<-randomforest(x = x,y = as.factor(y), ntree=100, norm.votes=false,importance = t) }
my way of parallelizing correct overall?
indeed.
the main problem you're not enclosing body of foreach loop in curly braces. because %dopar%
binary operator, have careful precedence, why recommend using curly braces.
also, shouldn't use c
combine function. since svm
, randomforest
return objects, default behavior of returning results in list appropriate. combining them c
give garbage result.
finally, doesn't make sense call registerdoparallel
twice. doesn't hurt, makes code confusing.
i suggest:
library(doparallel) no_cores <- detectcores() - 1 registerdoparallel(no_cores) model_list <- foreach(i = 1:2, .packages=c("e1071","randomforest")) %dopar% { if (i==1) { svm(x = x,y = as.factor(y),type = "c-classification", probability = t) } else { randomforest(x = x,y = as.factor(y), ntree=100, norm.votes=false, importance = t) } }
i removed 2 unnecessary variable assignments model1
, model2
. variables won't defined correctly on master, , obscures how foreach loop works.
Comments
Post a Comment