numpy数组与矩阵运算(二)

矩阵生成与常用操作

矩阵生成

扩展库numpy中提供的matrix()函数可以用来把列表、元组、range对象等Python可迭代对象转换为矩阵。

>>> import numpy as np
>>> x=np.matrix([[1,2,3],[4,5,6]])
>>> y=np.matrix([1,2,3,4,5,6])
>>> # 对矩阵x来说,x[1,1]和x[1][1]的含义不一样
>>> x
matrix([[1, 2, 3],
        [4, 5, 6]])
>>> y
matrix([[1, 2, 3, 4, 5, 6]])
>>> x[1,1]
5

矩阵转置

>>> x.T
matrix([[1, 4],
        [2, 5],
        [3, 6]])
>>> y.T
matrix([[1],
        [2],
        [3],
        [4],
        [5],
        [6]])

查看矩阵特性

>>> x=np.matrix([[1,2,3],[4,5,6]])
>>> x.mean() # 所有元素平均值
3.5
>>> x.mean(axis=0) # 纵向平均值
matrix([[2.5, 3.5, 4.5]])
>>> x.mean(axis=1) # 横向平均值
matrix([[2.],
        [5.]])
>>> x.sum() # 所有元素之和
21
>>> x.max(axis=1) # 横向最大值
matrix([[3],
        [6]])
>>> x.argmax(axis=1) # 横向最大值下标
matrix([[2],
        [2]], dtype=int64)
>>> x.diagonal() # 对角线元素
matrix([[1, 5]])
>>> x.nonzero() # 非0元素下标
(array([0, 0, 0, 1, 1, 1], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64))
>>> # 行下标列表和列下标列表

矩阵乘法

一个mxp的矩阵和一个pxn的矩阵,它们的乘积为一个mxn的矩阵

>>> x=np.matrix([[1,2,3],[4,5,6]])
>>> y=np.matrix([[1,2],[3,4],[5,6]])
>>> x*y
matrix([[22, 28],
        [49, 64]])

计算相关系数矩阵

>>> np.corrcoef([1,2,3,4],[4,3,2,1]) # 负相关,变化反向相反
array([[ 1., -1.],
       [-1.,  1.]])
>>> np.corrcoef([1,2,3,4],[8,3,2,1]) # 负相关,变化反向相反
array([[ 1.        , -0.91350028],
       [-0.91350028,  1.        ]])
>>> np.corrcoef([1,2,3,4],[1,2,3,4]) # 正相关,变化反向一致
array([[1., 1.],
       [1., 1.]])
>>> np.corrcoef([1,2,3,4],[1,2,3,40]) # 正相关,变化趋势接近
array([[1.       , 0.8010362],
       [0.8010362, 1.       ]])

计算方差、协方差、标准差

>>> np.cov([1,1,1,1,1]) # 方差
array(0.)
>>> np.std([1,1,1,1,1]) # 标准差
0.0
>>> x=[-2.1,-1,4.3]
>>> y=[3,1.1,0.12]
>>> X=np.vstack((x,y))
>>> X
array([[-2.1 , -1.  ,  4.3 ],
       [ 3.  ,  1.1 ,  0.12]])
>>> np.cov(X) # 协方差
array([[11.71      , -4.286     ],
       [-4.286     ,  2.14413333]])
>>> np.cov(x,y)
array([[11.71      , -4.286     ],
       [-4.286     ,  2.14413333]])
>>> np.std(X) # 标准差
2.2071223094538484
>>> np.std(X,axis=1)
array([2.79404128, 1.19558447])
>>> np.cov(x) # 方差
array(11.71)

计算特征值与特征向量

>>> A=np.array([[1,-3,3],[3,-5,3],[6,-6,4]])
>>> e,v=np.linalg.eig(A) # 特征值与特征向量
>>> e
array([ 4.+0.00000000e+00j, -2.+1.10465796e-15j, -2.-1.10465796e-15j])
>>> v
array([[-0.40824829+0.j        ,  0.24400118-0.40702229j,
         0.24400118+0.40702229j],
       [-0.40824829+0.j        , -0.41621909-0.40702229j,
        -0.41621909+0.40702229j],
       [-0.81649658+0.j        , -0.66022027+0.j        ,
        -0.66022027-0.j        ]])
>>> np.dot(A,v) # 矩阵与特征向量的乘积
array([[-1.63299316+0.00000000e+00j, -0.48800237+8.14044580e-01j,
        -0.48800237-8.14044580e-01j],
       [-1.63299316+0.00000000e+00j,  0.83243817+8.14044580e-01j,
         0.83243817-8.14044580e-01j],
       [-3.26598632+0.00000000e+00j,  1.32044054-5.55111512e-16j,
         1.32044054+5.55111512e-16j]])
>>> e*v # 特征值与特征向量的乘积
array([[-1.63299316+0.00000000e+00j, -0.48800237+8.14044580e-01j,
        -0.48800237-8.14044580e-01j],
       [-1.63299316+0.00000000e+00j,  0.83243817+8.14044580e-01j,
         0.83243817-8.14044580e-01j],
       [-3.26598632+0.00000000e+00j,  1.32044054-7.29317578e-16j,
         1.32044054+7.29317578e-16j]])
>>> np.isclose(np.dot(A,v),e*v) # 验证两者是否相等
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]])

计算逆矩阵

>>> x=np.matrix([[1,2,3],[4,5,6],[7,8,0]])
>>> y=np.linalg.inv(x) # 计算逆矩阵
>>> y
matrix([[-1.77777778,  0.88888889, -0.11111111],
        [ 1.55555556, -0.77777778,  0.22222222],
        [-0.11111111,  0.22222222, -0.11111111]])
>>> x*y # 对角线元素为1,其他元素为0或近似为0
matrix([[ 1.00000000e+00,  5.55111512e-17,  1.38777878e-17],
        [ 5.55111512e-17,  1.00000000e+00,  2.77555756e-17],
        [ 1.77635684e-15, -8.88178420e-16,  1.00000000e+00]])
>>> y*x
matrix([[ 1.00000000e+00, -1.11022302e-16,  0.00000000e+00],
        [ 8.32667268e-17,  1.00000000e+00,  2.22044605e-16],
        [ 6.93889390e-17,  0.00000000e+00,  1.00000000e+00]])

求解线性方程组

{ a 11 x 1 + a 12 x 2 + . . . + a 1 n x n = b 1 a 21 x 1 + a 22 x 2 + . . . + a 2 n x n = b 2 . . . a n 1 x 1 + a n 2 x 2 + . . . + a n n x n = b n \begin{cases} a11x1+a12x2+...+a1nxn=b1\\ a21x1+a22x2+...+a2nxn=b2\\ ...\\ an1x1+an2x2+...+annxn=bn\\ \end{cases} a11x1+a12x2+...+a1nxn=b1a21x1+a22x2+...+a2nxn=b2...an1x1+an2x2+...+annxn=bn
可以写作矩阵相乘的形式 ax=b
其中,a为nxn的矩阵,x和b为nx1的矩阵

>>> a=np.array([[3,1],[1,2]]) # 系数矩阵
>>> b=np.array([9,8]) # 系数矩阵
>>> x=np.linalg.solve(a,b) # 求解
>>> x
array([2., 3.])
>>> np.dot(a,x) # 验证
array([9., 8.])
>>> np.linalg.lstsq(a,b) # 最小二乘解,返回解、余项、a的秩、a的奇异值

Warning (from warnings module):
  File "<pyshell#77>", line 1
FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.
To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
(array([2., 3.]), array([], dtype=float64), 2, array([3.61803399, 1.38196601]))
>>>

有报错不要慌

>>> np.linalg.lstsq(a,b,rcond=None) # 最小二乘解,返回解、余项、a的秩、a的奇异值
(array([2., 3.]), array([], dtype=float64), 2, array([3.61803399, 1.38196601]))

可以写个方程去尝试一下,我试了一下,应该是没有问题的。

奇异值分解

把矩阵a分解为u*np.diag(s)*v的形式并返回u、s和v。其中数组s中的元素是矩阵a的元素值

>>> import numpy as np
>>> a=np.matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> u,s,v=np.linalg.svd(a) # 奇异值分解
>>> u
matrix([[-0.21483724,  0.88723069,  0.40824829],
        [-0.52058739,  0.24964395, -0.81649658],
        [-0.82633754, -0.38794278,  0.40824829]])
>>> s
array([1.68481034e+01, 1.06836951e+00, 4.41842475e-16])
>>> v
matrix([[-0.47967118, -0.57236779, -0.66506441],
        [-0.77669099, -0.07568647,  0.62531805],
        [-0.40824829,  0.81649658, -0.40824829]])
>>> u*np.diag(s)*v # 验证
matrix([[1., 2., 3.],
        [4., 5., 6.],
        [7., 8., 9.]])

函数向量化

>>> mat=np.matrix([[1,2,3],[4,5,6]])
>>> mat
matrix([[1, 2, 3],
        [4, 5, 6]])
>>> import math
>>> vecFactorial=np.vectorize(math.factorial) # 函数向量化
>>> vecFactorial(mat)
matrix([[  1,   2,   6],
        [ 24, 120, 720]])

猜你喜欢

转载自blog.csdn.net/weixin_46322367/article/details/129339012