Numpy array and matrix operations (2)

Matrix Generation and Common Operations

matrix generation

The functions provided in the extension library numpy matrix()can be used to convert Python iterable objects such as lists, tuples, and range objects into matrices.

>>> 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

Matrix transpose

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

View Matrix Properties

>>> 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))
>>> # 行下标列表和列下标列表

matrix multiplication

A xmatrix of m p and a xmatrix of p n, their product is a xmatrix of m n

>>> 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]])

Calculate the correlation coefficient matrix

>>> 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.       ]])

Calculate variance, covariance, standard deviation

>>> 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)

Calculate eigenvalues ​​and eigenvectors

>>> 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]])

Calculate the inverse matrix

>>> 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]])

Solve system of linear equations

{ 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} a 11 x 1+a 12 x 2+...+a 1 n x n=b 1a21x1+a 22 x 2+...+a 2 n x n=b 2...an 1 x 1+an2x2+...+ann x n=bn
It can be written in the form of matrix multiplication ax=b
where a is an nxn matrix, x and b are nx1 matrices

>>> 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]))
>>>

Don't panic if you get an error

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

You can write an equation to try it, I tried it, there should be no problem.

singular value decomposition

Decompose matrix a into the form u*np.diag(s)*v and return u, s, and v. The elements in the array s are the element values ​​of the matrix 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.]])

function vectorization

>>> 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]])

Guess you like

Origin blog.csdn.net/weixin_46322367/article/details/129339012