2011-02-09 237 views
10

我在R中有一個稀疏的Matrix,它對我來說運行as.matrix()顯然太大了(儘管它也不是超級巨大的)。有問題的as.matrix()調用在svd()函數中,所以我想知道是否有人知道不需要先轉換爲密集矩陣的SVD的不同實現。用於R中稀疏矩陣的SVD

+0

R找不到任何東西。C,Fortran,Python等有很多東西。 – 2011-02-09 22:38:42

+0

也許我會試用SVDLIBC。它構建爲一個C庫,所以如果它運行良好,我將來可以將它作爲一個模塊來包裝(儘管我的雄心可能不會持續那麼久,如果歷史是任何指導的話)。 – 2011-02-10 17:32:25

+2

這個http://cran.r-project.org/web/packages/irlba/如何計算一個快速和有效的記憶方法來計算一些近似奇異值和大矩陣的奇異向量。 – 2012-02-23 08:29:31

回答

10

irlba包具有非常快的SVD實現稀疏矩陣。

6

這就是我最終做的。這是比較簡單的編寫轉儲稀疏矩陣(類dgCMatrix)在SVDLIBC的「疏文本」格式的文本文件的程序,然後調用svd可執行文件,並閱讀這三個結果的文本文件放回R.

的趕上它是相當低效的 - 我需要大約10秒才能讀取&寫入文件,但實際的SVD計算僅需要大約0.2秒左右。儘管如此,這當然比不能完成計算要好得多,所以我很高興。 =)

+0

後續問題張貼在http://stackoverflow.com/questions/5009026/extract-long-from-r-object。 – 2011-02-15 21:11:45

+1

在不提供任何示例數據或解決方案代碼的情況下給自己一個複選標記似乎與SO的原理相悖。 – 2012-09-29 15:29:49

8

可以使用隨機投影做稀疏SVD的非常可觀的位中的R爲在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)) 
} 
3

rARPACK是您需要的軟件包。像魅力一樣工作,超快,因爲它通過C和C++並行化。