1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| M = 3 N = 5 (mat = replicate(N, 1:M))
(vec = 1:N)
(sweep(mat, MARGIN=2, vec,`+`))
(sweep(mat, MARGIN=2, vec,`*`))
(sweep(mat, MARGIN=2, vec,`/`))
(sweep(mat, MARGIN=2, vec,`-`))
(sweep(mat, MARGIN=2, vec, pmax))
(sweep(mat, MARGIN=2, vec, pmin))
N = 10; p = 3 dat = matrix(rnorm(N*p), N)
dat_standardized = sweep(sweep(dat, 2, colMeans(dat)), 2, apply(dat, 2, sd), '/')
dat_normalized = sweep(sweep(dat, 2, apply(dat, 2, min)), 2, apply(dat, 2, function(x) diff(range(x))), '/')
kernelMatrix_f = function(X, center, sigma){ exp(sweep(sweep(X %*% t(center), 1, rowSums(X**2)/2), 2, rowSums(center**2)/2) / (sigma**2)) }
library(kernlab) library(Rcpp) library(RcppArmadillo) sourceCpp(code = ' // [[Rcpp::depends(RcppArmadillo)]] #include <RcppArmadillo.h> using namespace Rcpp; using namespace arma;
// [[Rcpp::export]] NumericMatrix kernelMatrix_cpp(NumericMatrix Xr, NumericMatrix Centerr, double sigma) { uword n = Xr.nrow(), b = Centerr.nrow(), row_index, col_index; mat X(Xr.begin(), n, Xr.ncol(), false), Center(Centerr.begin(), b, Centerr.ncol(), false), KerX(X*Center.t()); colvec X_sq = sum(square(X), 1) / 2; rowvec Center_sq = (sum(square(Center), 1)).t() / 2; KerX.each_row() -= Center_sq; KerX.each_col() -= X_sq; KerX *= 1 / (sigma * sigma); KerX = exp(KerX); return wrap(KerX); }')
N = 8000 p = 500 b = 1000 X = matrix(rnorm(N*p), ncol = p) center = X[sample(1:N, b),] sigma = 3
all.equal(kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center)@.Data, kernelMatrix_cpp(X, center, sigma))
all.equal(kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center)@.Data, kernelMatrix_f(X, center, sigma))
library(microbenchmark) microbenchmark(Rfun = kernelMatrix_f(X, center, sigma), kernlab = kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center), Rcpp = kernelMatrix_cpp(X, center, sigma), times = 20L )
|