低。有许多种方法可以提升你的代码运算效率,但或许你更想了解运算效率能得到多大的提升。本文将介绍几种适用于大数据领域的方法,包括简单的逻辑调整设计、并行处理和Rcpp的运用,利用这些方法你可以轻松地处理1亿行以上的数据集。
让我们尝试提升往数据框中添加一个新变量过程(该过程中包含循环和判断语句)的运算效率。下面的代码输出原始数据框:

  1. # Create the data frame
  2. col1 <- runif (12^5, 0, 2)
  3. col2 <- rnorm (12^5, 0, 2)
  4. col3 <- rpois (12^5, 3)
  5. col4 <- rchisq (12^5, 2)
  6. df <- data.frame (col1, col2, col3, col4)

复制代码

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

  1. # Original R code: Before vectorization and pre-allocation
  2. system.time({
  3. for (i in 1:nrow(df)) { # for every row
  4. if ((df[i, 'col1'] + df[i, 'col2'] + df[i, 'col3'] + df[i, 'col4']) > 4) { # check if > 4
  5. df[i, 5] <- "greater_than_4" # assign 5th column
  6. } else {
  7. df[i, 5] <- "lesser_than_4" # assign 5th column
  8. }
  9. }
  10. })

复制代码

本文中所有的计算都在配置了2.6Ghz处理器和8GB内存的MAC OS X中运行。

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

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

  1. # after vectorization and pre-allocation
  2. output <- character (nrow(df)) # initialize output vector
  3. system.time({
  4. for (i in 1:nrow(df)) {
  5. if ((df[i, 'col1'] + df[i, 'col2'] + df[i, 'col3'] + df[i, 'col4']) > 4) {
  6. output[i] <- "greater_than_4"
  7. } else {
  8. output[i] <- "lesser_than_4"
  9. }
  10. }
  11. df$output})

复制代码

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

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

  1. # after vectorization and pre-allocation, taking the condition checking outside the loop.
  2. output <- character (nrow(df))
  3. condition <- (dfcol1+dfcol2 + dfcol3+dfcol4) > 4  # condition check outside the loop
  4. system.time({
  5. for (i in 1:nrow(df)) {
  6. if (condition[i]) {
  7. output[i] <- "greater_than_4"
  8. } else {
  9. output[i] <- "lesser_than_4"
  10. }
  11. }
  12. df$output <- output
  13. })

复制代码

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

另一种优化方法是预先将输出变量赋值为条件语句不满足时的取值,然后只在条件语句为真时执行循环过程。此时,运算速度的提升程度取决于条件状态中真值的比例。
本部分的测试将和case(2)部分进行比较,和预想的结果一致,该方法确实提升了运算效率。

  1. output <- c(rep("lesser_than_4", nrow(df)))
  2. condition <- (dfcol1+dfcol2 + dfcol3+dfcol4) > 4
  3. system.time({
  4. for (i in (1:nrow(df))[condition]) {  # run loop only for true conditions
  5. if (condition[i]) {
  6. output[i] <- "greater_than_4"
  7. }
  8. }
  9. df$output
  10. })

复制代码

4.尽可能地使用 ifelse()语句

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

  1. system.time({
  2. output <- ifelse ((dfcol1+dfcol2 + dfcol3+dfcol4) > 4, "greater_than_4", "lesser_than_4")
  3. df$output <- output
  4. })

复制代码

5.使用 which()语句

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

  1. # Thanks to Gabe Becker
  2. system.time({
  3. want = which(rowSums(df) > 4)
  4. output = rep("less than 4", times = nrow(df))
  5. output[want] = "greater than 4"
  6. })
  7. # nrow = 3 Million rows (approx)
  8. user  system elapsed
  9. 0.396   0.074   0.481

复制代码

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

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

  1. # apply family
  2. system.time({
  3. myfunc <- function(x) {
  4. if ((x['col1'] + x['col2'] + x['col3'] + x['col4']) > 4) {
  5. "greater_than_4"
  6. } else {
  7. "lesser_than_4"
  8. }
  9. }
  10. output <- apply(df[, c(1:4)], 1, FUN=myfunc)  # apply 'myfunc' on every row
  11. df$output <- output
  12. })

复制代码

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

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

  1. # byte code compilation
  2. library(compiler)
  3. myFuncCmp <- cmpfun(myfunc)
  4. system.time({
  5. output <- apply(df[, c (1:4)], 1, FUN=myFuncCmp)
  6. })

复制代码

8.利用Rcpp

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

  1. library(Rcpp)
  2. sourceCpp("MyFunc.cpp")
  3. system.time (output <- myFunc(df)) # see Rcpp function below

复制代码

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

  1. // Source for MyFunc.cpp
  2. #include
  3. using namespace Rcpp;
  4. // [[Rcpp::export]]
  5. CharacterVector myFunc(DataFrame x) {
  6. NumericVector col1 = as(x["col1"]);
  7. NumericVector col2 = as(x["col2"]);
  8. NumericVector col3 = as(x["col3"]);
  9. NumericVector col4 = as(x["col4"]);
  10. int n = col1.size();
  11. CharacterVector out(n);
  12. for (int i=0; i 4){
  13. out[i] = "greater_than_4";
  14. } else {
  15. out[i] = "lesser_than_4";
  16. }
  17. }
  18. return out;
  19. }

复制代码

9.利用并行运算

并行运算的代码:

  1. # parallel processing
  2. library(foreach)
  3. library(doSNOW)
  4. cl <- makeCluster(4, type="SOCK") # for 4 cores machine
  5. registerDoSNOW (cl)
  6. condition <- (dfcol1+dfcol2 + dfcol3+dfcol4) > 4
  7. # parallelization with vectorization
  8. system.time({
  9. output <- foreach(i = 1:nrow(df), .combine=c) %dopar% {
  10. if (condition[i]) {
  11. return("greater_than_4")
  12. } else {
  13. return("lesser_than_4")
  14. }
  15. }
  16. })
  17. df$output <- output

复制代码

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

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

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

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

  1. dt <- data.table(df)  # create the data.table
  2. system.time({
  3. for (i in 1:nrow (dt)) {
  4. if ((dt[i, col1] + dt[i, col2] + dt[i, col3] + dt[i, col4]) > 4) {
  5. dt[i, col5:="greater_than_4"]  # assign the output as 5th column
  6. } else {
  7. dt[i, col5:="lesser_than_4"]  # assign the output as 5th column
  8. }
  9. }
  10. })

复制代码

总结

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

原始方法:1X, 856.2255行每秒(正则化为1)
向量化方法:738X, 631578行每秒
只考虑真值情况:1002X,857142.9行每秒
ifelse:1752X,1500000行每秒
which:8806X,7540364行每秒
Rcpp:13476X,11538462行每秒

原文作者:Selva Prabhakaran
原文链接:http://datartisan.com/article/detail/82.html
译者:fibears

【R】提升R代码运算效率的11个实用方法的更多相关文章

  1. 提升R代码运算效率的11个实用方法

    提升R代码运算效率的11个实用方法 众所周知,当我们利用R语言处理大型数据集时,for 循环语句的运算效率非常低.有许多种方法可以提升你的代码运算效率,但或许你更想了解运算效率能得到多大的提升.本文将 ...

  2. 提升R代码运算效率的11个实用方法——并行、效率

    转载于36大数据,原文作者:Selva Prabhakaran  译者:fibears 众所周知,当我们利用R语言处理大型数据集时,for循环语句的运算效率非常低.有许多种方法可以提升你的代码运算效率 ...

  3. R︱并行计算以及提高运算效率的方式(parallel包、clusterExport函数、SupR包简介)

    要学的东西太多,无笔记不能学~~ 欢迎关注公众号,一起分享学习笔记,记录每一颗"贝壳"~ --------------------------- 终于开始攻克并行这一块了,有点小兴 ...

  4. iOS:Xcode代码块,提升敲代码的效率

    一.代码块在哪里? 看下图 或者 快捷键:command+shift+L 长这样: 二.如何创建代码块: 1.先选中要创建的代码片段,然后点击右键,选中 Create Code Snippet 然后会 ...

  5. 随机森林入门攻略(内含R、Python代码)

    随机森林入门攻略(内含R.Python代码) 简介 近年来,随机森林模型在界内的关注度与受欢迎程度有着显著的提升,这多半归功于它可以快速地被应用到几乎任何的数据科学问题中去,从而使人们能够高效快捷地获 ...

  6. R语言的日期运算

    写hive SQL查询, 需要从导入的参数, 自动累加日期. 从而实现一个自动的,多个日期的统计过程 R语言的日期运算超级简单. > test<-Sys.Date() > test ...

  7. 决策树ID3原理及R语言python代码实现(西瓜书)

    决策树ID3原理及R语言python代码实现(西瓜书) 摘要: 决策树是机器学习中一种非常常见的分类与回归方法,可以认为是if-else结构的规则.分类决策树是由节点和有向边组成的树形结构,节点表示特 ...

  8. R树-javascript代码实现过程分析(插入操作)

    R Tree 第一步,创建R树类. 构建一个RTree生成器.用以创建tree对象. 例子:var tree = new RTree(12) var RTree = function(width){ ...

  9. R(一): R基础知识

    R 是一门拥有统计分析及作图功能的免费软件,主要用于数学建模.统计计算.数据处理.可视化等方向.据 IEEE Spectrum发布的2016年编程语言前10位排名来看,R语言由2015年排名第6位上升 ...

随机推荐

  1. Openresty增加waf配置

    Openresty增加waf配置 1. Ngx lua waf 说明 防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击 防止svn/备份之类文件泄漏 防止Apach ...

  2. 定时器quartz工具类

    一.gradle配置 // https://mvnrepository.com/artifact/org.quartz-scheduler/quartz compile group: 'org.qua ...

  3. VirtualBOX 不能mount优盘,移动硬盘解决方案

    The Solution (basically nayasis' solution with the second driver installation added): Safely unplug ...

  4. springboot http2

    转 Spring Boot With Http2 原文链接 http://www.jianshu.com/p/3d7ac535d6a0 拓展链接 http://www.jianshu.com/p/97 ...

  5. 定制WinEdt 优化Latex输入

    定制WinEdt 优化Latex输入 ---原文转载地址:http://www.mamicode.com/info-detail-342716.html ---推荐winedt功能定制使用-说明文档: ...

  6. wavwrite注意事项

    前几天群里有人提出一个问题:MATLAB里,同样频率的信号写入/读取,为什么频率感觉不同? 测试code: fs = 2000; f0 = 20; t = 0:1/fs:1; subplot 211 ...

  7. centos升级glibc动态库

    glibc是gnu发布的libc库,即c运行库,glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc.glibc除了封装linux操作系统所提供的系统服务外,它本身也提供 ...

  8. iptables控制较复杂案例

    场景设定: 管理员:192.168.101.80 公司有三个部门: 工程部:192.168.2.21-192.168.2.20 软件部门:192.168.2.21-192.168.2.30 经理办公室 ...

  9. linux-CentOS6.4安装Memcached+memcached扩展+安装memcache扩展+Memcache+mecached同步SESSION的几种方法

    一.编译环境的准备 yum install gcc  yum install gcc-c++ libstdc++-devel  yum install zlib-devel 二.源代码包准备 wget ...

  10. Monkey源代码分析番外篇之Android注入事件的三种方法比較

    原文:http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/#.VEoIoIuUcaV 往下分析 ...