R - 更快地分类基质产品

时间:2017-06-20 22:49:19

标签: r sorting matrix

有2个矩阵A和B. A的大小为2M * 50,B为20k * 50。我想为每一行计算A%*%t(B)的前10个值。 我想知道是否有比这个更快的实现

library(parallel)
library(pbapply)

set.seed(1)

A <- matrix(runif(2e6*50), nrow=2e6)
B <- matrix(runif(2e4*50), nrow=2e4)
n <- 10

cl = makeCluster(detectCores())

clusterExport(cl, c("A","B", "n"))

Z <- pbsapply(1:nrow(A), function(x){
  score = A[x,] %*% t(B)
  nth_score = -sort(-score, partial=n)[n]
  top_scores_1 = which(score > nth_score)
  top_scores_2 = which(score == nth_score)
  if (!length(top_scores_2) == 1)  top_scores_2 = sample(top_scores_2, n - length(top_scores_1))
  top_scores = c(top_scores_1, top_scores_2)
  top_ix = sort(score[top_scores], decreasing = T, index.return=T)$ix
  return(top_scores[top_ix])
}, cl = cl)

stopCluster(cl)

1 个答案:

答案 0 :(得分:1)

一项快速改进,将A %*% t(B)替换为tcrossprod(A,B)

n <- 10

func1 <- function(x){
  score = A[x,] %*% t(B)
  nth_score = -sort(-score, partial=n)[n]
  top_scores_1 = which(score > nth_score)
  top_scores_2 = which(score == nth_score)
  if (!length(top_scores_2) == 1)  top_scores_2 = sample(top_scores_2, n - length(top_scores_1))
  top_scores = c(top_scores_1, top_scores_2)
  top_ix = sort(score[top_scores], decreasing = T, index.return=T)$ix
  return(top_scores[top_ix])
}

func2 <- function(x){
  score = tcrossprod(A[x,],B)
  nth_score = -sort(-score, partial=n)[n]
  top_scores_1 = which(score > nth_score)
  top_scores_2 = which(score == nth_score)
  if (!length(top_scores_2) == 1)  top_scores_2 = sample(top_scores_2, n - length(top_scores_1))
  top_scores = c(top_scores_1, top_scores_2)
  top_ix = sort(score[top_scores], decreasing = T, index.return=T)$ix
  return(top_scores[top_ix])
}

all.equal(func1(1),func2(1))
# TRUE

microbenchmark(func1(1),func2(1))
# Unit: milliseconds
# expr      min       lq     mean   median        uq       max neval
# func1(1) 6.527077 9.254476 9.757431 9.726585 10.311310 11.932170   100
# func2(1) 3.365654 3.721711 4.036532 3.998387  4.246175  5.405226   100