Python 引发 LinAlgError("SVD 没有收敛") LinAlgError: SVD 在 matplotlib pca 确定中没有收敛
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21827594/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
raise LinAlgError("SVD did not converge") LinAlgError: SVD did not converge in matplotlib pca determination
提问by user 3317704
code :
代码 :
import numpy
from matplotlib.mlab import PCA
file_name = "store1_pca_matrix.txt"
ori_data = numpy.loadtxt(file_name,dtype='float', comments='#', delimiter=None,                 converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
result = PCA(ori_data)
this is my code. though my input matrix is devoid of the nan and inf, i do get the error stated below.
这是我的代码。尽管我的输入矩阵没有 nan 和 inf,但我确实收到了下面所述的错误。
raise LinAlgError("SVD did not converge") LinAlgError: SVD did not converge
what's the problem?
有什么问题?
采纳答案by jseabold
This can happen when there are inf or nan values in the data.
当数据中有 inf 或 nan 值时,就会发生这种情况。
Use this to remove nan values:
使用它来删除 nan 值:
ori_data.dropna(inplace=True)
回答by Ji?í Polcar
I do not have an answer to this question but I have the reproduction scenario with no nans and infs. Unfortunately the datataset is pretty large (96MB gzipped).
我对这个问题没有答案,但我有没有 nans 和 infs 的复制场景。不幸的是,数据集非常大(压缩后为 96MB)。
import numpy as np
from StringIO import StringIO
from scipy import linalg
import urllib2
import gzip
url = 'http://physics.muni.cz/~vazny/gauss/X.gz'
X = np.loadtxt(gzip.GzipFile(fileobj=StringIO(urllib2.urlopen(url).read())), delimiter=',')
linalg.svd(X, full_matrices=False)
which rise:
其中上升:
LinAlgError: SVD did not converge
on:
在:
>>> np.__version__
'1.8.1'
>>> import scipy
>>> scipy.__version__
'0.10.1'
but did not raise an exception on:
但没有在以下方面引发异常:
>>> np.__version__
'1.8.2'
>>> import scipy
>>> scipy.__version__
'0.14.0'
回答by Sumit Waghmare
This may be due to the singular nature of your input datamatrix (which you are feeding to PCA)
这可能是由于您的输入数据矩阵的奇异性质(您将其提供给 PCA)
回答by nos
I am using numpy 1.11.0. If the matrix has more than 1 eigvalues equal to 0, then 'SVD did not converge' is raised.
我正在使用 numpy 1.11.0。如果矩阵有超过 1 个等于 0 的特征值,则会引发“SVD 未收敛”。
回答by Slava
Even if your data is correct, it may happen because it runs out of memory. In my case, moving from a 32-bit machine to a 64-bit machine with bigger memory solved the problem.
即使你的数据是正确的,它也可能因为内存不足而发生。就我而言,从 32 位机器迁移到具有更大内存的 64 位机器解决了这个问题。
回答by Vlamir
I know this post is old, but in case someone else encounters the same problem. @jseabold was right when he said that the problem is nan or inf and the op was probably right when he said that the data did not have nan's or inf. However, if one of the columns in ori_data has always the same value, the data will get Nans, since the implementation of PCA in mlab normalizes the input data by doing
我知道这篇文章很旧,但以防万一其他人遇到同样的问题。@jseabold 说问题是 nan 或 inf 是对的,当他说数据没有 nan 或 inf 时,op 可能是对的。但是,如果 ori_data 中的列之一始终具有相同的值,则数据将得到 Nans,因为 mlab 中 PCA 的实现通过执行以下操作来规范化输入数据
ori_data = (ori_data - mean(ori_data)) / std(ori_data).
The solution is to do:
解决办法是这样做:
result = PCA(ori_data, standardize=False)
In this way, only the mean will be subtracted without dividing by the standard deviation.
这样,只减去平均值而不除以标准差。
回答by hevronig
Following on @c-chavez answer, what worked for me was first replacing inf and -inf to nan, then removing nan. For example:
按照@c-chavez 的回答,对我有用的是首先将 inf 和 -inf 替换为 nan,然后删除 nan。例如:
data = data.replace(np.inf, np.nan).replace(-np.inf, np.nan).dropna()
回答by Paritosh Gupta
If there are no inf or NaN values, possibly that is a memory issue. Please try in a machine with higher RAM.
如果没有 inf 或 NaN 值,则可能是内存问题。请在具有更高 RAM 的机器上尝试。
回答by chenjesu
This happened to me when I accidentally resized an image dataset to (0, 64, 3). Try checking the shape of your dataset to see if one of the dimensions is 0.
当我不小心将图像数据集调整为 (0, 64, 3) 时,发生了这种情况。尝试检查数据集的形状以查看其中一个维度是否为 0。

