Search code examples
pythonnumpylinear-regression

LinAlgError: Singular matrix when solving linalg.inv with numpy


By executing np.linalg.inv(S) I get always an error:

The original S is

matrix([[matrix([[6371.]]), matrix([[6371.]])],
        [matrix([[6371.]]), matrix([[6371.]])]], dtype=object)

TypeError: No loop matching the specified signature and casting was found for ufunc inv

if I convert s to float64 S = S.astype(np.float64) the content of S is

matrix([[6371., 6371.],
        [6371., 6371.]])

LinAlgError: Singular matrix 

How can I solve this issue? I know the determinant is 0, however, by using other approaches like SVD and QR I also get errors.


Solution

  • Try the following after converting S to float64:

    result = np.linalg.pinv(S)
    

    This returns the pseudo inverse.