如何在R中截断SVD

时间:2016-08-25 18:46:33

标签: r machine-learning svd

我有两个矩阵,traintest。我如何适应" train上的奇异值分解并将拟合变换应用于test

例如

library(irlba)

# train
train <- cbind(matrix(runif(16, min=0, max=1), nrow=8), 
               matrix(runif(16, min=30, max=31), nrow=8))
train[1:4, ] = train[1:4, ] + 50

# test
test <- cbind(matrix(runif(16, min=0, max=1), nrow=8), 
              matrix(runif(16, min=30, max=31), nrow=8))
test[1:4, ] = test[1:4, ] + 50

# trunacted SVD applied to train
S <- irlba(t(train), nv=2)

> train
         [,1]    [,2]  [,3]  [,4]
[1,] 50.39686 50.8733 80.57 80.51
[2,] 50.42719 50.2288 80.64 80.17
[3,] 50.87391 50.6059 80.19 80.61
[4,] 50.52439 50.7037 80.59 80.36
[5,]  0.43121  0.4681 30.93 30.76
[6,]  0.69381  0.5647 30.12 30.11
[7,]  0.02068  0.3382 30.37 30.04
[8,]  0.61101  0.5401 30.12 30.86

> S$v
       [,1]     [,2]
[1,] 0.4819  0.23134
[2,] 0.4805  0.18348
[3,] 0.4816  0.07372
[4,] 0.4816 -0.05819
[5,] 0.1370 -0.59769
[6,] 0.1342 -0.20746
[7,] 0.1335 -0.70946
[8,] 0.1358 -0.01972

现在,如何减少test的尺寸? (另请注意,我的真实数据集很大且很稀疏。)

0 个答案:

没有答案