R 中稀疏矩阵的 SVD

发布于 2024-10-16 16:58:42 字数 154 浏览 5 评论 0原文

我在 R 中有一个稀疏矩阵,它显然太大了,无法在其上运行 as.matrix()(尽管它也不是超级大)。有问题的 as.matrix() 调用位于 svd() 函数内部,所以我想知道是否有人知道 SVD 的不同实现,不需要首先转换为稠密矩阵。

I've got a sparse Matrix in R that's apparently too big for me to run as.matrix() on (though it's not super-huge either). The as.matrix() call in question is inside the svd() function, so I'm wondering if anyone knows a different implementation of SVD that doesn't require first converting to a dense matrix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

×纯※雪 2024-10-23 16:58:43

这就是我最终所做的。编写一个例程相对简单,将稀疏矩阵(类 dgCMatrix)转储到 SVDLIBC 的“稀疏文本”格式的文本文件,然后调用 svd 可执行文件,并读取 。

问题是它的效率非常低——我需要大约 10 秒的时间来读取和读取结果 写入文件,但实际SVD计算只需要大约0.2秒左右。不过,这当然比根本无法执行计算要好得多,所以我很高兴。 =)

So here's what I ended up doing. It's relatively straightforward to write a routine that dumps a sparse matrix (class dgCMatrix) to a text file in SVDLIBC's "sparse text" format, then call the svd executable, and read the three resultant text files back into R.

The catch is that it's pretty inefficient - it takes me about 10 seconds to read & write the files, but the actual SVD calculation takes only about 0.2 seconds or so. Still, this is of course way better than not being able to perform the calculation at all, so I'm happy. =)

眼泪也成诗 2024-10-23 16:58:43

rARPACK 是您需要的包。工作起来非常神奇,而且速度超快,因为它通过 C 和 C++ 进行并行化。

rARPACK is the package you need. Works like a charm and is Superfast because it parallelizes via C and C++.

不打扰别人 2024-10-23 16:58:42

irlba 包具有非常快速的稀疏矩阵 SVD 实现。

The irlba package has a very fast SVD implementation for sparse matrices.

画骨成沙 2024-10-23 16:58:42

您可以使用随机投影在 R 中进行非常令人印象深刻的稀疏 SVD,如 http://arxiv.org/abs 中所述/0909.4061

这是一些示例代码:

# computes first k singular values of A with corresponding singular vectors
incore_stoch_svd = function(A, k) {
  p = 10              # may need a larger value here
  n = dim(A)[1]
  m = dim(A)[2]

  # random projection of A    
  Y = (A %*% matrix(rnorm((k+p) * m), ncol=k+p))
  # the left part of the decomposition works for A (approximately)
  Q = qr.Q(qr(Y))
  # taking that off gives us something small to decompose
  B = t(Q) %*% A

  # decomposing B gives us singular values and right vectors for A  
  s = svd(B)
  U = Q %*% s$u
  # and then we can put it all together for a complete result
  return (list(u=U, v=s$v, d=s$d))
}

You can do a very impressive bit of sparse SVD in R using random projection as described in http://arxiv.org/abs/0909.4061

Here is some sample code:

# computes first k singular values of A with corresponding singular vectors
incore_stoch_svd = function(A, k) {
  p = 10              # may need a larger value here
  n = dim(A)[1]
  m = dim(A)[2]

  # random projection of A    
  Y = (A %*% matrix(rnorm((k+p) * m), ncol=k+p))
  # the left part of the decomposition works for A (approximately)
  Q = qr.Q(qr(Y))
  # taking that off gives us something small to decompose
  B = t(Q) %*% A

  # decomposing B gives us singular values and right vectors for A  
  s = svd(B)
  U = Q %*% s$u
  # and then we can put it all together for a complete result
  return (list(u=U, v=s$v, d=s$d))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文