【问题标题】:How to increase the perfomance of R in machine learning?如何提高 R 在机器学习中的性能?
【发布时间】:2016-10-25 08:36:53
【问题描述】:

我同时使用 R 和 python(Anaconda)。在建模方面,R 需要更多时间来执行相同的操作。R 中有没有并行操作或多线程的方法?

【问题讨论】:

    标签: python r performance machine-learning regression


    【解决方案1】:

    在 R 中使用机器学习算法时,您可以调用一些允许并行化的库。在下面的示例中,差异是巨大的。

    您可以这样称呼它们(注意“2”取决于您的集群数量):

    library(doParallel)
    cl <- makeCluster(2)
    registerDoParallel(cl)
    

    本例摘自https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf

    > x <- iris[which(iris[,5] != "setosa"), c(1,5)]
    > trials <- 10000
    > ptime <- system.time({
    + 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)
    + }
    + })[3]
    > ptime 
    

    而且比

    快得多
    > stime <- system.time({
    + r <- foreach(icount(trials), .combine=cbind) %do% {
    + ind <- sample(100, 100, replace=TRUE)
    + result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
    + coefficients(result1)
    + }
    + })[3]
    > stime
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 2020-08-08
      • 2021-04-09
      • 2017-02-23
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多