代码记录
library(RMySQL)
library(nnet)
library(plyr)
library(lattice)
library(e1071)
library(foreach)
library(doParallel)
#read info from sql
sql_data<-"select distinct(MP_ID) from DataPower"
conn<-dbConnect(MySQL(),user="",password="",dbname="powerloaddata",host="") #开启数据库连接
data_info<-dbGetQuery(conn,sql_data) #获得原始数据 611363856
dbDisconnect(conn) #关闭数据库连接
mp_id = as.numeric(data_info[,1])
# 启用parallel作为foreach并行计算的后端
cl <- makeCluster(20)
registerDoParallel(cl)
rm(meanDataForeach)
foreach (x=1:length(mp_id)) %dopar% {
trainAndtest(mp_id[x])
}
关于foreach的效率问题
看了stackoverflow
发现foreach在多循环的情况下效率不高。
原话是:
foreach is only advisable if you have relatively few rounds through very time consuming functions.