Ken*_*ams 10 r sparse-matrix svd
我的Matrix
R 稀疏,显然对我来说太大as.matrix()
了(虽然它也不是超大).有as.matrix()
问题的调用是在svd()
函数内部,所以我想知道是否有人知道不需要先转换为密集矩阵的SVD的不同实现.
你可以使用随机投影在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))
}
Run Code Online (Sandbox Code Playgroud)
所以这就是我最终做的事情.编写dgCMatrix
以SVDLIBC的"稀疏文本"格式将稀疏矩阵(类)转储到文本文件的例程相对简单,然后调用svd
可执行文件,并将三个结果文本文件读回R.
问题在于效率非常低 - 我需要大约10秒来读取和写入文件,但实际的SVD计算只需要大约0.2秒左右.不过,这当然比完全无法执行计算要好,所以我很高兴.=)
归档时间: |
|
查看次数: |
8483 次 |
最近记录: |