Using Numpy (np.linalg.svd) for Singular Value Decomposition

前端 未结 3 1061
既然无缘
既然无缘 2021-02-02 12:01

Im reading Abdi & Williams (2010) \"Principal Component Analysis\", and I\'m trying to redo the SVD to attain values for further PCA.

The article states that followi

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 12:26

    I think there are still some important points for those who use SVD in Python/linalg library. Firstly, https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.svd.html is a good reference for SVD computation function.

    Taking SVD computation as A= U D (V^T), For U, D, V = np.linalg.svd(A), this function returns V in V^T form already. Also D contains eigenvalues only, hence it has to be shaped into matrix form. Hence the reconstruction can be formed with

    import numpy as np
    U, D, V = np.linalg.svd(A)
    A_reconstructed = U @ np.diag(D) @ V
    

    The point is that, If A matrix is not a square but rectangular matrix, this won't work, you can use this instead

    import numpy as np
    U, D, V = np.linalg.svd(A)
    m, n = A.shape
    A_reconstructed = U[:,:n] @ np.diag(D) @ V[:m,:]
    

    or you may use 'full_matrices=False' option in the SVD function;

    import numpy as np
    U, D, V = np.linalg.svd(A,full_matrices=False)
    A_reconstructed = U @ np.diag(D) @ V
    

提交回复
热议问题