我想在r中生成30000 x 30000的矩阵,通过其转置乘以30000个元素的向量,然后获得该矩阵的SVD,但程序告诉我r无法找到大小为900000000的向量。我,我该怎么办?
y <- read.csv("C:\\Users\\jmarescr\\Desktop\\BigLetra50.csv",header=TRUE)
x <- matrix(y[1:30000,1],30000,1)
tx <- as.matrix(t(x))
mat <- x %*% tx
Error: can not allocate vector of length 900000000
s <- svd(mat)
Error in svd (x): object 'mat' not found
答案 0 :(得分:7)
SVD的一部分优点是你不需要采用x
的交叉产品来获得交叉产品的SVD。
您可以直接从x%*%t(x)
的SVD元素中获取tcrossprod(x)
(又名x
)的SVD。具体而言(直到U列的符号)SVD(x%*%t(x))= U D ^ 2 t(U),其中U和D取自x
的SVD。 (供参考,see here。)
要查看它的实际效果,请尝试一个较小的示例:
set.seed(1)
x <- matrix(rnorm(15), ncol=5)
svd(x)$d
# [1] 3.046842 1.837539 1.411585
sqrt(svd(tcrossprod(x))$d)
# [1] 3.046842 1.837539 1.411585
svd(x)$u
# [,1] [,2] [,3]
# [1,] -0.3424029 0.7635281 0.5475264
# [2,] -0.8746155 -0.4719093 0.1111273
# [3,] 0.3432316 -0.4408248 0.8293766
svd(tcrossprod(x))$u
# [,1] [,2] [,3]
# [1,] -0.3424029 0.7635281 0.5475264
# [2,] -0.8746155 -0.4719093 0.1111273
# [3,] 0.3432316 -0.4408248 0.8293766
svd(tcrossprod(x))$v
# [,1] [,2] [,3]
# [1,] -0.3424029 0.7635281 0.5475264
# [2,] -0.8746155 -0.4719093 0.1111273
# [3,] 0.3432316 -0.4408248 0.8293766
另一种看法:
sss <- svd(x)
with(sss, u %*% diag(d)^2 %*% t(u))
# [,1] [,2] [,3]
# [1,] 3.654154 1.684675 -1.322649
# [2,] 1.684675 7.877802 -1.900721
# [3,] -1.322649 -1.900721 3.120415
tcrossprod(x)
# [,1] [,2] [,3]
# [1,] 3.654154 1.684675 -1.322649
# [2,] 1.684675 7.877802 -1.900721
# [3,] -1.322649 -1.900721 3.120415