python矩阵分解

矩阵的奇异值分解

import numpy as np
aa= np.array([[1, 1], [1, -2], [2, 1]])
bb=np.linalg.svd(aa)
print(bb)
(array([[ -5.34522484e-01,  -1.11022302e-16,  -8.45154255e-01],
       [  2.67261242e-01,  -9.48683298e-01,  -1.69030851e-01],
       [ -8.01783726e-01,  -3.16227766e-01,   5.07092553e-01]]), array([ 2.64575131,  2.23606798]), array([[-0.70710678, -0.70710678],
       [-0.70710678,  0.70710678]]))

矩阵的LU分解

m=np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]])
import scipy

df=scipy.linalg.lu(m, permute_l=True)
L=df[0]
U=df[1]
L
Out[129]: 
array([[ 0.14285714,  1.        ,  0.        ],
       [ 0.57142857,  0.5       ,  1.        ],
       [ 1.        ,  0.        ,  0.        ]])
U
Out[130]: 
array([[  7.00000000e+00,   8.00000000e+00,   9.00000000e+00],
       [  0.00000000e+00,   8.57142857e-01,   1.71428571e+00],
       [  0.00000000e+00,   0.00000000e+00,   1.11022302e-16]])
L@U
Out[131]: 
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])

矩阵的Cholesky分解

import scipy
import numpy
m=numpy.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]])

qr = scipy.linalg.qr(m)

qr[0]@qr[1]
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])

你可能感兴趣的:(算法)