Open Triamus opened 6 years ago
library(caret) library(microbenchmark) library(doParallel)
cores_2_use <- floor(0.8 * detectCores()) cl <- makeCluster(cores_2_use, outfile = "parallel_log1.txt") registerDoParallel(cl)
x <- iris[which(iris[,5] != "setosa"), c(1,5)] trials <- 100 temp <- microbenchmark( r <- foreach(icount(trials), .combine=cbind) %dopar% { ind <- sample(100, 100, replace=TRUE) result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit)) coefficients(result1)} )
parallel::stopCluster(cl) foreach::registerDoSEQ()
set.seed(123) x <- data.frame( target = as.factor(rep(c("no", "yes"), 5)), pred1 = rnorm(10), pred2 = letters[seq(1, 10, 1)] )
x1 = rnorm(100) # some continuous variables x2 = rnorm(100) z = 1 + 2x1 + 3x2 # linear combination with a bias pr = 1/(1+exp(-z)) # pass through an inv-logit function y = rbinom(100,1,pr) # bernoulli response variable df = data.frame(y = as.factor(ifelse(y == 0, "no", "yes")), x1 = x1, x2 = x2)
ctrl <- trainControl( method = "repeatedcv", number = 10, repeats = 5, classProbs = TRUE, summaryFunction = twoClassSummary)
microbenchmark( glm_nopar = train(y ~ ., data = df, method = "glm", family = "binomial", metric = "ROC", trControl = ctrl), times = 5)
cores_2_use <- floor(0.8 * detectCores()) cl <- makeCluster(cores_2_use, outfile = "parallel_log2.txt") registerDoParallel(cl)
microbenchmark( glm_par = train(y ~ ., data = df, method = "glm", family = "binomial", metric = "ROC", trControl = ctrl), times = 5)
library(caret) library(microbenchmark) library(doParallel)
cores_2_use <- floor(0.8 * detectCores()) cl <- makeCluster(cores_2_use, outfile = "parallel_log1.txt") registerDoParallel(cl)
x <- iris[which(iris[,5] != "setosa"), c(1,5)] trials <- 100 temp <- microbenchmark( r <- foreach(icount(trials), .combine=cbind) %dopar% { ind <- sample(100, 100, replace=TRUE) result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit)) coefficients(result1)} )
parallel::stopCluster(cl) foreach::registerDoSEQ()
mock-up data
set.seed(123) x <- data.frame( target = as.factor(rep(c("no", "yes"), 5)), pred1 = rnorm(10), pred2 = letters[seq(1, 10, 1)] )
x1 = rnorm(100) # some continuous variables x2 = rnorm(100) z = 1 + 2x1 + 3x2 # linear combination with a bias pr = 1/(1+exp(-z)) # pass through an inv-logit function y = rbinom(100,1,pr) # bernoulli response variable df = data.frame(y = as.factor(ifelse(y == 0, "no", "yes")), x1 = x1, x2 = x2)
train control function
ctrl <- trainControl( method = "repeatedcv", number = 10, repeats = 5, classProbs = TRUE, summaryFunction = twoClassSummary)
train function
microbenchmark( glm_nopar = train(y ~ ., data = df, method = "glm", family = "binomial", metric = "ROC", trControl = ctrl), times = 5)
trying in parallel
cores_2_use <- floor(0.8 * detectCores()) cl <- makeCluster(cores_2_use, outfile = "parallel_log2.txt") registerDoParallel(cl)
microbenchmark( glm_par = train(y ~ ., data = df, method = "glm", family = "binomial", metric = "ROC", trControl = ctrl), times = 5)