R_Language 板


LINE

[關鍵字]: bsxfun in R [出處]: http://stackoverflow.com/questions/3643555/multiply-rows-of-matrix-by-vector [重點摘要]: pastebin link: http://pastebin.com/jGtHeYuj M = 3 N = 5 (mat = replicate(N, 1:M)) # [,1] [,2] [,3] [,4] [,5] # [1,] 1 1 1 1 1 # [2,] 2 2 2 2 2 # [3,] 3 3 3 3 3 (vec = 1:N) # [1] 1 2 3 4 5 (sweep(mat, MARGIN=2, vec,`+`)) # [,1] [,2] [,3] [,4] [,5] # [1,] 2 3 4 5 6 # [2,] 3 4 5 6 7 # [3,] 4 5 6 7 8 (sweep(mat, MARGIN=2, vec,`*`)) # [,1] [,2] [,3] [,4] [,5] # [1,] 1 2 3 4 5 # [2,] 2 4 6 8 10 # [3,] 3 6 9 12 15 (sweep(mat, MARGIN=2, vec,`/`)) # [,1] [,2] [,3] [,4] [,5] # [1,] 1 0.5 0.3333333 0.25 0.2 # [2,] 2 1.0 0.6666667 0.50 0.4 # [3,] 3 1.5 1.0000000 0.75 0.6 (sweep(mat, MARGIN=2, vec,`-`)) # [,1] [,2] [,3] [,4] [,5] # [1,] 0 -1 -2 -3 -4 # [2,] 1 0 -1 -2 -3 # [3,] 2 1 0 -1 -2 (sweep(mat, MARGIN=2, vec, pmax)) # [,1] [,2] [,3] [,4] [,5] # [1,] 1 2 3 4 5 # [2,] 2 2 3 4 5 # [3,] 3 3 3 4 5 (sweep(mat, MARGIN=2, vec, pmin)) # [,1] [,2] [,3] [,4] [,5] # [1,] 1 1 1 1 1 # [2,] 1 2 2 2 2 # [3,] 1 2 3 3 3 ## usage in scaling 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))), '/') ## usage in computing kernel kernelMatrix_f = function(X, center, sigma){ exp(sweep(sweep(X %*% t(center), 1, rowSums(X**2)/2), 2, rowSums(center**2)/2) / (sigma**2)) } ## compare with Rcpp and kernlab 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)) # TRUE all.equal(kernelMatrix(rbfdot(sigma=1/(2*sigma^2)), X, center)@.Data, kernelMatrix_f(X, center, sigma)) # TRUE 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 ) # Unit: milliseconds # expr min lq mean median uq max neval # Rfun 872.8854 981.1995 1036.2209 1074.0526 1098.9185 1132.9356 20 # kernlab 773.7489 841.9028 1059.3098 862.7476 883.3874 2979.2541 20 # Rcpp 490.2462 501.2993 520.1283 522.5060 532.5426 571.0949 20 速度不輸package: kernlab。 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.225.212.167
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1429191476.A.F82.html
1F:→ Wush978: 這不知道能不能用BLAS算 04/16 22:51
DGER可以做 A = alpha*x*y' + A (+, -的case) alpha: constant, x: m by 1, y: 1 by n, A: m by n x 對應到上面的vec y 是全部都1的向量 A 是mat alpha = 1 http://tinyurl.com/mqwv2hc
2F:→ Wush978: 就我的理解,你應該直接#include "cblas.h"然後和R 04/17 01:50
3F:→ Wush978: linking就可以了。因為R應該內帶blas 04/17 01:50
4F:→ Wush978: 另外沒意外的話,用blas可能會是最快的算法 04/17 01:51
不過現在看起來BLAS能做的case只有+/-,*或是/或是min, max都無法
5F:推 penolove5566: 謝分享 04/17 02:20
6F:→ clickhere: 那是fortran函式,用 include "R_ext/BLAS.h" 04/17 02:53
也試過了 ※ 編輯: celestialgod (36.225.212.167), 04/17/2015 14:43:28







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP