提升R代码运算效率的11中方法


当我们利用R语言处理大型数据集时,for循环语句的运算效率非常低。有许多种方法可以提升你的代码运算效率,但或许你更想了解运算效率能得到多大的提升。本文将介绍几种适用于大数据领域的方法,包括简单的逻辑调整设计、并行处理和Rcpp的运用,利用这些方法你可以轻松地处理1亿行以上的数据集。

输出原始数据框:

#Create the data frame

col1<- runif (12^5, 0, 2)

col2<- rnorm (12^5, 0, 2)

col3<- rpois (12^5, 3)

col4<- rchisq (12^5, 2)

df<- data.frame (col1, col2, col3, col4)

> dim(df)
[1] 248832      5

 

对数据框进行处理:

逐行判断该数据框(df)的总和是否大于4,如果该条件满足,则对应的新变量数值为’greaterthan4’,否则赋值为’lesserthan4’。

system.time({

  for(i in 1:nrow(df)){

    if((df[i, 'col1']+df[i, 'col2']+df[i,'col3']+df[i, 'col4'])>4){

      df[i, 5] <- "greater_than_4"

    }else{

      df[i, 5] <- "lesser_than_4"

    }

  }

})

用户   系统   流逝

#180.48  44.64 225.21

 

1.向量化处理和预设数据库结构

循环运算前,记得预先设置好数据结构和输出变量的长度和类型,千万别在循环过程中渐进性地增加数据长度。接下来,我们将探究向量化处理是如何提高处理数据的运算速度

#预分配初始化向量

output<- character(nrow(df))

system.time({

  for(i in 1:nrow(df)){

    if((df[i, 'col1']+df[i, 'col2']+df[i,'col3']+df[i, 'col4'])>4){

      output[i] <-"greater_than_4"

    } else{

      output[i] <- "lesser_than_4"

    }

  }

  df$output

})

 用户 系统  流逝
15.61  0.00 15.61

 

2.将条件语句的判断条件移至循环外

将条件判断语句移至循环外可以提升代码的运算速度,接下来本文将利用包含100,000行数据至1,000,000行数据的数据集进行测试:

将条件语句的判断条件移至循环外

# after vectorization and pre-allocation, taking thecondition checking outside the loop.

output <- character(nrow(df))              #预分配初始化向量

condition <- (df$col1+df$col2+df$col3+df$col4)>4      # condition check outside the loop

system.time({

  for(i in 1:nrow(df)){

    if(condition[i]){

      output[i] <-"greater_than_4"

    }else{

      output[i] <- "lesser_than_4"

    }

  }

  df$output <- output

})

用户 系统 流逝
0.39 0.00 0.40

 

3、只在条件语句为真时执行循环过程

另一种优化方法是预先将输出变量赋值为条件语句不满足时的取值,然后只在条件语句为真时执行循环过程。此时,运算速度的提升程度取决于条件状态中真值的比例。

只在条件语句为真时执行循环过程

output <- c(rep("lesser_than_4", nrow(df)))             #全部初始化为“lesser_than_4

condition <- (df$col1+df$col2+df$col3+df$col4)>4    #大于4T,小于4F

system.time({

  for(i in (1:nrow(df))[condition]){        #对为T的数据进行循环

    if(condition[i]){        

      output[i] <-"greater_than_4"

    }

  }

  df$output

})

用户 系统 流逝
0.33 0.00 0.33

 

4.尽可能地使用 ifelse()语句:最佳的提升方法

利用ifelse()语句可以使你的代码更加简便。ifelse()的句法格式类似于if()函数,但其运算速度却有了巨大的提升。即使是在没有预设数据结构且没有简化条件语句的情况下,其运算效率仍高于上述的两种方法。

system.time({

  output <-ifelse((df$col1+df$col2+df$col3+df$col4)>4, "greater_than_4", "lesser_than_4")

  df$output <- output

})

用户 系统 流逝
0.08 0.02 0.10

 

5.使用 which()语句

利用which()语句来筛选数据集,我们可以达到Rcpp三分之一的运算速率。

system.time({

  want <- which(rowSums(df)>4)

  output <- rep("less_than_4", times=nrow(df))

  output[want] = "greater_than_4"

})

用户 系统 流逝
0.02 0.00 0.02

 

system.time({

  want <- which(rowSums(df)>4)

  df$output <- rep("less_than_4", times=nrow(df))

  df$output[want] = "greater_than_4"

})

用户 系统 流逝
0.01 0.00 0.01

 

6.利用apply族函数来替代for循环语句

本部分将利用apply()函数来计算上文所提到的案例,并将其与向量化的循环语句进行对比。该方法的运算效率优于原始方法,但劣于ifelse()和将条件语句置于循环外端的方法。该方法非常有用,但是当你面对复杂的情形时,你需要灵活运用该函数。

# apply family

system.time({

  myfunc <- function(x){

   if((x['col1']+x['col2']+x['col3']+x['col4'])>4){

      "greater_than_4"

    } else{

      "lesser_than_4"

    }

  }

  output <- apply(df[,c(1:4)], 1, FUN = myfunc)    # apply 'myfunc' on every row

  df$output <- output

})

用户 系统 流逝
1.42 0.00 1.42

 

7.利用compiler包中的字节码编译函数cmpfun()

这可能不是说明字节码编译有效性的最好例子,但是对于更复杂的函数而言,字节码编译将会表现地十分优异,因此我们应当了解下该函数。

# byte code compilation

library(compiler)

myFuncCmp <-cmpfun(myfunc)

system.time({

  output <- apply(df[,c(1:4)], 1, FUN =myFuncCmp)

})

用户 系统 流逝
1.10 0.00 1.09

 

8.利用Rcpp

截至目前,我们已经测试了好几种提升运算效率的方法,其中最佳的方法是利用ifelse()函数。如果我们将数据量增大十倍,运算效率将会变成啥样的呢?接下来我们将利用Rcpp来实现该运算过程,并将其与ifelse()进行比较。

library(Rcpp)

sourceCpp("MyFunc.cpp")

system.time(output <- myFunc(df)) # see Rcpp function below

下面是利用C++语言编写的函数代码,将其保存为“MyFunc.cpp”并利用sourceCpp进行调用。

//Source for MyFunc.cpp

#include 

usingnamespace Rcpp;

//[[Rcpp::export]]

CharacterVectormyFunc(DataFrame x) {

  NumericVector col1 = as(x["col1"]);

  NumericVector col2 = as(x["col2"]);

  NumericVector col3 = as(x["col3"]);

  NumericVector col4 = as(x["col4"]);

  int n = col1.size();

  CharacterVector out(n);

  for (int i=0; i 4){

    out[i] = "greater_than_4";

  } else {

    out[i] = "lesser_than_4";

  }

}

returnout;

}

#注意:此处的C++程序好像是有问题,没有深入研究,如果为了提升效率,还要写C++程序,是有点麻烦,很多人不会C++

 

9.利用并行运算

#parallel processing

library(foreach)

library(iterators)

library(snow)

library(doSNOW)

cl<- makeCluster(4, type="SOCK") # for 4 cores machine

registerDoSNOW(cl)

condition<- (df$col1 + df$col2 + df$col3 + df$col4) > 4

#parallelization with vectorization

system.time({

  output <- foreach(i = 1:nrow(df), .combine=c) %dopar%{

    if (condition[i]) {

      return("greater_than_4")

    } else {

      return("lesser_than_4")

    }

  }

})

df$output<- output

注意:使用此方法一直卡着没有运行处结果,1000多秒后强制停止!看来R的并行还是有问题的?请高手指点!

 

10.尽早地移除变量并恢复内存容量

在进行冗长的循环计算前,尽早地将不需要的变量移除掉。在每次循环迭代运算结束时利用gc()函数恢复内存也可以提升运算速率。

 

11.利用内存较小的数据结构

data.table()是一个很好的例子,因为它可以减少数据的内存,这有助于加快运算速率。

library(data.table)

dt <- data.table(df)   #data.frame创建data.table

system.time({

  for(i in 1:nrow(dt)){

    if((dt[i, col1]+dt[i, col2]+dt[i,col3]+dt[i, col4])>4){

      dt[i,col5:="greater_than_4"]   #分配输出为第五列

    }else{

      dt[i,col5:="lesser_than_4"]    #分配输出为第五列

    }

  }

})

  用户  系统   流逝
431.61   0.01 431.81

注意:此方式提升的效率不明显,在此次测试中反而没有data.frame的速度快呢。

 

总结

方法:速度, nrow(df)/time_taken= n 行每秒

1.原始方法:1X,856.2255行每秒(正则化为1)

2.向量化方法:738X, 631578行每秒

3.只考虑真值情况:1002X,857142.9行每秒

4.ifelse:1752X,1500000行每秒

5.which:8806X,7540364行每秒

6.Rcpp:13476X,11538462行每秒

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值