其他分享
首页 > 其他分享> > Numpy_matrix

Numpy_matrix

作者:互联网

import numpy as np

矩阵生成

x = np.matrix([[1,2,3],[4,5,6]])
y = np.matrix([1,2,3,4,5,6])
print(x,y,x[0,0],x[0])  #数组中 x[1][1] 和 x[1,1]  表示同一个元素 矩阵不是
[[1 2 3]
 [4 5 6]] [[1 2 3 4 5 6]] 1 [[1 2 3]]

矩阵转置

x = np.mat([[1,2,3],[4,5,6]])
y = np.mat([1,2,3,4,5,6])
x.T
matrix([[1, 4],
        [2, 5],
        [3, 6]])
y.T
matrix([[1],
        [2],
        [3],
        [4],
        [5],
        [6]])
x
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=0).shape #数组形状
(1, 3)
x.mean(axis=1) #横向平均值
matrix([[2.],
        [5.]])
x.max(axis=1) #横向最大值
matrix([[3],
        [6]])
x.diagonal() #对角线元素
matrix([[1, 5]])
x.nonzero() #非零元素下标 分别返回行下标,列下标
(array([0, 0, 0, 1, 1, 1], dtype=int64),
 array([0, 1, 2, 0, 1, 2], dtype=int64))

矩阵乘法

x = np.matrix([[1,2,3],[4,5,6]])
y = np.matrix([[1,2],[3,4],[5,6]])
print(x)
[[1 2 3]
 [4 5 6]]
print(y)
[[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.794041278626117
np.std(X,axis=1)
array([2.79404128, 1.19558447])
np.cov(x) #方差
array(11.71)

特征值与特征向量

A = np.mat([[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
matrix([[-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) #矩阵和特征向量乘积
matrix([[-1.63299316+0.0000000e+00j, -0.48800237+8.1404458e-01j,
         -0.48800237-8.1404458e-01j],
        [-1.63299316+0.0000000e+00j,  0.83243817+8.1404458e-01j,
          0.83243817-8.1404458e-01j],
        [-3.26598632+0.0000000e+00j,  1.32044054-4.4408921e-16j,
          1.32044054+4.4408921e-16j]])
np.isclose(np.dot(A,v),e*v) #验证二者是否相等
matrix([[False, False, False],
        [False, False, False],
        [False, False, False]])
np.linalg.det(A-np.eye(3,3)*e)  #det 计算行列式的函数 |A-(蓝不大)*E|
(-3.660807638291249e-30+5.965152994198125e-14j)

计算逆矩阵

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,  1.11022302e-16,  0.00000000e+00],
        [-1.11022302e-15,  1.00000000e+00,  0.00000000e+00],
        [ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00]])
y*x
matrix([[ 1.00000000e+00, -4.44089210e-16,  0.00000000e+00],
        [ 2.22044605e-16,  1.00000000e+00,  0.00000000e+00],
        [ 1.11022302e-16,  1.11022302e-16,  1.00000000e+00]])

求解线性方程组

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的奇异值
<ipython-input-44-018dd4c0b16e>: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`.
  np.linalg.lstsq(a,b) #最小二乘解  返回解余项 a的秩 a的奇异值





(array([2., 3.]), array([], dtype=float64), 2, array([3.61803399, 1.38196601]))

计算向量和矩阵的范数

x = np.matrix([[1,2],[3,-4]])
np.linalg.norm(x)
5.477225575051661
np.linalg.norm(x,-2)
1.9543950758485487
np.linalg.norm([1,2,0,3,4],2)
5.477225575051661

奇异值分解

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
# math.factorial(mat) 会报错
vecfactorial = np.vectorize(math.factorial) #函数向量化
vecfactorial(mat)
matrix([[  1,   2,   6],
        [ 24, 120, 720]])

标签:00,False,matrix,linalg,np,array,Numpy
来源: https://www.cnblogs.com/lingxiaoyu/p/15465177.html