R-svd()函数-'x'中的无穷或缺失值 [英] R - svd() function - infinite or missing values in 'x'
本文介绍了R-svd()函数-'x'中的无穷或缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在收到此错误.我确定矩阵中没有任何非数字条目.我还尝试过估算矩阵,没有用.
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));
推荐答案
您有由全零组成的列.在全零的列上使用scale
会返回由NaN
组成的列.要解决此问题,请删除全为零的列(svd
不会显示任何有关其的新内容),或者在使用scale
后将NaN
列替换为零.
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
.
可复制的示例:
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
这篇关于R-svd()函数-'x'中的无穷或缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文