Getting the singular values of np.linalg.svd as a matrix

后端 未结 1 2026

Given a 5x4 matrix A =

A piece of python code to construct the matrix

A = np.array([[1, 0, 0, 0],
              [0, 0, 0, 4],
              [0,          


        
相关标签:
1条回答
  • 2021-01-23 06:22

    You can get most of the way there with diag:

    >>> u, s, vh = np.linalg.svd(a)
    >>> np.diag(s)
    array([[ 4.        ,  0.        ,  0.        ,  0.        ],
           [ 0.        ,  3.        ,  0.        ,  0.        ],
           [ 0.        ,  0.        ,  2.23606798,  0.        ],
           [ 0.        ,  0.        ,  0.        , -0.        ]])
    

    Note that wolfram alpha is giving an extra row. Getting that is marginally more involved:

    >>> sigma = np.zeros(A.shape, s.dtype)
    >>> np.fill_diagonal(sigma, s)
    >>> sigma
    array([[ 4.        ,  0.        ,  0.        ,  0.        ],
           [ 0.        ,  3.        ,  0.        ,  0.        ],
           [ 0.        ,  0.        ,  2.23606798,  0.        ],
           [ 0.        ,  0.        ,  0.        , -0.        ],
           [ 0.        ,  0.        ,  0.        ,  0.        ]])
    

    Depending on what your goal is, removing a column from U might be a better approach than adding a row of zeros to sigma. That would look like:

    >>> u, s, vh = np.linalg.svd(a, full_matrices=False)
    
    0 讨论(0)
提交回复
热议问题