celestialgod

bsxfun in R

Apr 16th, 2015
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.25 KB | None | 0 0
  1. M = 3
  2. N = 5
  3. (mat = replicate(N, 1:M))
  4. #      [,1] [,2] [,3] [,4] [,5]
  5. # [1,]    1    1    1    1    1
  6. # [2,]    2    2    2    2    2
  7. # [3,]    3    3    3    3    3
  8. (vec = 1:N)
  9. # [1] 1 2 3 4 5
  10. (sweep(mat, MARGIN=2, vec,`+`))
  11. #      [,1] [,2] [,3] [,4] [,5]
  12. # [1,]    2    3    4    5    6
  13. # [2,]    3    4    5    6    7
  14. # [3,]    4    5    6    7    8
  15. (sweep(mat, MARGIN=2, vec,`*`))
  16. #      [,1] [,2] [,3] [,4] [,5]
  17. # [1,]    1    2    3    4    5
  18. # [2,]    2    4    6    8   10
  19. # [3,]    3    6    9   12   15
  20. (sweep(mat, MARGIN=2, vec,`/`))
  21. #      [,1] [,2]      [,3] [,4] [,5]
  22. # [1,]    1  0.5 0.3333333 0.25  0.2
  23. # [2,]    2  1.0 0.6666667 0.50  0.4
  24. # [3,]    3  1.5 1.0000000 0.75  0.6
  25. (sweep(mat, MARGIN=2, vec,`-`))
  26. #      [,1] [,2] [,3] [,4] [,5]
  27. # [1,]    0   -1   -2   -3   -4
  28. # [2,]    1    0   -1   -2   -3
  29. # [3,]    2    1    0   -1   -2
  30. (sweep(mat, MARGIN=2, vec, pmax))
  31. #      [,1] [,2] [,3] [,4] [,5]
  32. # [1,]    1    2    3    4    5
  33. # [2,]    2    2    3    4    5
  34. # [3,]    3    3    3    4    5
  35. (sweep(mat, MARGIN=2, vec, pmin))
  36. #      [,1] [,2] [,3] [,4] [,5]
  37. # [1,]    1    1    1    1    1
  38. # [2,]    1    2    2    2    2
  39. # [3,]    1    2    3    3    3
  40.  
  41. ## usage in scaling
  42. N = 10; p = 3
  43. dat = matrix(rnorm(N*p), N)
  44.  
  45. dat_standardized = sweep(sweep(dat, 2, colMeans(dat)), 2,
  46.   apply(dat, 2, sd), '/')
  47.  
  48. dat_normalized = sweep(sweep(dat, 2, apply(dat, 2, min)), 2,
  49.   apply(dat, 2, function(x) diff(range(x))), '/')
  50.  
  51. ## usage in computing kernel matrix
  52. kernelMatrix_f = function(X, center, sigma){
  53.   exp(sweep(sweep(X %*% t(center), 1,
  54.     rowSums(X**2)/2), 2, rowSums(center**2)/2) / (sigma**2))
  55. }
  56.  
  57. ## compare with Rcpp and kernlab
  58. library(kernlab)
  59. library(Rcpp)
  60. library(RcppArmadillo)
  61. sourceCpp(code = '
  62. // [[Rcpp::depends(RcppArmadillo)]]
  63. #include <RcppArmadillo.h>
  64. using namespace Rcpp;
  65. using namespace arma;
  66.  
  67. // [[Rcpp::export]]
  68. NumericMatrix kernelMatrix_cpp(NumericMatrix Xr, NumericMatrix Centerr, double sigma) {
  69.  uword n = Xr.nrow(), b = Centerr.nrow(), row_index, col_index;
  70.  mat X(Xr.begin(), n, Xr.ncol(), false),
  71.      Center(Centerr.begin(), b, Centerr.ncol(), false),
  72.      KerX(X*Center.t());
  73.  colvec X_sq = sum(square(X), 1) / 2;
  74.  rowvec Center_sq = (sum(square(Center), 1)).t() / 2;
  75.  KerX.each_row() -= Center_sq;
  76.  KerX.each_col() -= X_sq;
  77.  KerX *= 1 / (sigma * sigma);
  78.  KerX = exp(KerX);
  79.  return wrap(KerX);
  80. }')
  81.  
  82. N = 8000
  83. p = 500
  84. b = 1000
  85. X = matrix(rnorm(N*p), ncol = p)
  86. center = X[sample(1:N, b),]
  87. sigma = 3
  88.  
  89. all.equal(kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center)@.Data,
  90.   kernelMatrix_cpp(X, center, sigma))
  91. # TRUE
  92. all.equal(kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center)@.Data,
  93.   kernelMatrix_f(X, center, sigma))
  94. # TRUE
  95.  
  96. library(microbenchmark)
  97. microbenchmark(Rfun = kernelMatrix_f(X, center, sigma),
  98.   kernlab = kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center),
  99.   Rcpp = kernelMatrix_cpp(X, center, sigma),
  100.   times = 20L
  101. )
  102. # Unit: milliseconds
  103. #     expr      min       lq      mean    median        uq       max neval
  104. #     Rfun 872.8854 981.1995 1036.2209 1074.0526 1098.9185 1132.9356    20
  105. #  kernlab 773.7489 841.9028 1059.3098  862.7476  883.3874 2979.2541    20
  106. #     Rcpp 490.2462 501.2993  520.1283  522.5060  532.5426  571.0949    20
Advertisement
Add Comment
Please, Sign In to add comment