Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

raise LinAlgError("SVD did not converge") LinAlgError: SVD did not converge in matplotlib pca determination

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.

raise LinAlgError("SVD did not converge") LinAlgError: SVD did not converge

what's the problem?

like image 575
user 3317704 Avatar asked Feb 17 '14 11:02

user 3317704


4 Answers

This can happen when there are inf or nan values in the data.

Use this to remove nan values:

ori_data.dropna(inplace=True)
like image 190
jseabold Avatar answered Oct 22 '22 18:10

jseabold


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

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.

like image 38
Vlamir Avatar answered Oct 22 '22 19:10

Vlamir


If there are no inf or NaN values, possibly that is a memory issue. Please try in a machine with higher RAM.

like image 10
Paritosh Gupta Avatar answered Oct 22 '22 17:10

Paritosh Gupta


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).

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'
like image 7
Jiří Polcar Avatar answered Oct 22 '22 18:10

Jiří Polcar