R - svd() function - infinite or missing values in 'x'

≯℡__Kan透↙ 提交于 2019-12-17 16:39:36

问题


I am constantly getting this error. I am sure the matrix does not have any non-numeric entries. I also tried imputing the matrix, did not work.

Anyone know what the error might be?

fileUrl <- "https://dl.dropboxusercontent.com/u/76668273/kdd.csv";
download.file(fileUrl,destfile="./kdd.csv",method="curl");
kddtrain <- read.csv("kdd.csv");
kddnumeric <- kddtrain[,sapply(kddtrain,is.numeric)];
kddmatrix <- as.matrix(kddnumeric);
svd1 <- svd(scale(kddmatrix));

回答1:


You have columns composed of all zeroes. Using scale on a column of all zeroes returns a column composed of NaN. To solve this, remove columns where you have all zeroes (svd will not reveal anything new about them), or replace NaN columns with zero after using scale.

Reproducible example:

mat <- matrix(c(1,2,3,0,0,0,2,4,6,5,12,13),nrow = 3)
     # [,1] [,2] [,3] [,4]
# [1,]    1    0    2    5
# [2,]    2    0    4   12
# [3,]    3    0    6   13
scale(mat)
     # [,1] [,2] [,3]       [,4]
# [1,]   -1  NaN   -1 -1.1470787
# [2,]    0  NaN    0  0.4588315
# [3,]    1  NaN    1  0.6882472
# attr(,"scaled:center")
# [1]  2  0  4 10
# attr(,"scaled:scale")
# [1] 1.000000 0.000000 2.000000 4.358899
svd(mat) #fine
svd(scale(mat)) # not fine


来源:https://stackoverflow.com/questions/21423375/r-svd-function-infinite-or-missing-values-in-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!