当前位置:   article > 正文

【Python】【numpy-汇总10】numpy.linalg函数的示例代码_python中linalg函数

python中linalg函数

1.np.linalg所有函数

函数说明
det(ndarray)计算矩阵列式
eig(ndarray)计算方阵的本征值和本征向量

inv(ndarray)

pinv(ndarray)

计算方阵的逆

计算方阵的Moore-Penrose伪逆

qr(ndarray)计算qr分解 
svd(ndarray)计算奇异值分解svd
solve(ndarray)解线性方程组Ax = b,其中A为方阵 
lstsq(ndarray)计算Ax=b的最小二乘解 

2.使用示例

det计算方阵的行列式


  1. #det(ndarray) 计算方阵的矩阵列式
  2. a = np.array([[1,6,7],
  3. [2,5,3],
  4. [3,4,1]])
  5. ret = np.linalg.det(a) #-14

eig计算方阵的本征值和本征向量

  1. a = np.array([[1,2,3],
  2. [6,5,4],
  3. [7,6,5]])
  4. ret = np.linalg.eig(a) #本征值,本征向量
  5. print(ret[0]) #[ 1.27284161e+01 -1.72841615e+00 1.90757877e-16]
  6. print(ret[1]) #[[ 0.29245036 0.78800111 0.40824829]
  7. # [ 0.60875015 -0.45826232 -0.81649658]
  8. # [ 0.73749308 -0.41115678 0.40824829]]

inv 计算方阵的逆

  1. a = np.array([[1,2,3],
  2. [4,5,6],
  3. [7,8,9]])
  4. ai = np.linalg.inv(a)
  5. print(ai)
  6. print(ai.shape)
  7. '''
  8. [[ -4.50359963e+15 9.00719925e+15 -4.50359963e+15]
  9. [ 9.00719925e+15 -1.80143985e+16 9.00719925e+15]
  10. [ -4.50359963e+15 9.00719925e+15 -4.50359963e+15]]
  11. (3, 3)
  12. '''

pinv 计算方阵的Moore-Penrose伪逆

  1. api = np.linalg.pinv(a)
  2. print(api)
  3. print(api.shape)
  4. '''
  5. [[ -6.38888889e-01 -1.66666667e-01 3.05555556e-01]
  6. [ -5.55555556e-02 3.46944695e-17 5.55555556e-02]
  7. [ 5.27777778e-01 1.66666667e-01 -1.94444444e-01]]
  8. (3, 3)
  9. '''

qr计算qr分解 

  1. a = np.array([[1,2,3],
  2.               [4,5,6],
  3.               [7,8,9]])
  4. aqr = np.linalg.qr(a)  
  5. print(aqr)
  6. '''
  7. (array([[-0.12309149,  0.90453403,  0.40824829],
  8.        [-0.49236596,  0.30151134, -0.81649658],
  9.        [-0.86164044, -0.30151134,  0.40824829]]), 
  10. array([[ -8.12403840e+00,  -9.60113630e+00,  -1.10782342e+01],
  11.        [  0.00000000e+00,   9.04534034e-01,   1.80906807e+00],
  12.        [  0.00000000e+00,   0.00000000e+00,  -7.58790979e-16]]))
  13. '''

svd计算奇异值分解svd

  1. asvd = np.linalg.svd(a)
  2. print(asvd)
  3. '''
  4. (array([[-0.21483724, 0.88723069, 0.40824829],
  5. [-0.52058739, 0.24964395, -0.81649658],
  6. [-0.82633754, -0.38794278, 0.40824829]]),
  7. array([ 1.68481034e+01, 1.06836951e+00, 4.41842475e-16]),
  8. array([[-0.47967118, -0.57236779, -0.66506441],
  9. [-0.77669099, -0.07568647, 0.62531805],
  10. [-0.40824829, 0.81649658, -0.40824829]]))
  11. '''

solve解线性方程组Ax = b,其中A为方阵 

  1. a = np.array([[1,2,3],
  2. [4,5,6],
  3. [7,8,9]])
  4. b = np.array([0,2,6])
  5. ret = np.linalg.solve(a,b)
  6. print(ret)
  7. '''
  8. (array([[-0.21483724, 0.88723069, 0.40824829],
  9. [-0.52058739, 0.24964395, -0.81649658],
  10. [-0.82633754, -0.38794278, 0.40824829]]),
  11. array([ 1.68481034e+01, 1.06836951e+00, 4.41842475e-16]),
  12. array([[-0.47967118, -0.57236779, -0.66506441],
  13. [-0.77669099, -0.07568647, 0.62531805],
  14. [-0.40824829, 0.81649658, -0.40824829]]))
  15. [ -9.00719925e+15 1.80143985e+16 -9.00719925e+15]
  16. '''

lstsq计算Ax=b的最小二乘解 

  1. a = np.array([[1,2,3],
  2. [4,5,6],
  3. [7,8,9]])
  4. b = np.array([0,2,6])
  5. ret = np.linalg.lstsq(a,b)
  6. print('lstsq:\n',ret)
  7. '''
  8. lstsq:
  9. (array([ 1.5 , 0.33333333, -0.83333333]),
  10. array([], dtype=float64), 2,
  11. array([ 1.68481034e+01, 1.06836951e+00, 4.41842475e-16]))
  12. '''

(end)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/119061
推荐阅读
相关标签
  

闽ICP备14008679号