Kkk
Kkk

Reputation: 59

LinAlgError: SVD did not converge in Linear Least Squares np.linalg.lstsq

I get the aforementioned error although I check that I have no missing or infinite values when trying to do linear regression with np.linalg.lstsq...Any thoughts? I do these regressions recursively and I printed the shape of X and Y and are always same but it raises the error in a specific iteration

---> 17             betas = np.linalg.lstsq(X,Y,rcond=None)[0]
     18 
     19             res = Y - X.dot(betas)

<__array_function__ internals> in lstsq(*args, **kwargs)

~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in lstsq(a, b, rcond)
   2257         # lapack can't handle n_rhs = 0 - so allocate the array one larger in that axis
   2258         b = zeros(b.shape[:-2] + (m, n_rhs + 1), dtype=b.dtype)
-> 2259     x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)
   2260     if m == 0:
   2261         x[...] = 0

~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in _raise_linalgerror_lstsq(err, flag)
    107 
    108 def _raise_linalgerror_lstsq(err, flag):
--> 109     raise LinAlgError("SVD did not converge in Linear Least Squares")
    110 
    111 def get_linalg_error_extobj(callback):

LinAlgError: SVD did not converge in Linear Least Squares

Upvotes: 3

Views: 2780

Answers (1)

NotAName
NotAName

Reputation: 4322

There are two most common reasons for this error. One is when you have infinity or NaNs present in your data. You say you check for those but do you check on each iteration or only in the original data?

If that is not the issue then the other possible problem is that a set of linear equations can have 1 solution (think like a point between two intersecting lines), but it can also have no solutions (for example, parallel lines) or infinite number of solutions (two lines on top of each other). The latter two situations should also trigger this error since the function cannot come up with a definitive answer.

Upvotes: 2

Related Questions