本篇介绍Numpy中的与矩阵(Matrix)相关的常用函数。
1. 直接创建
>>> A = np.mat([[1,2],[3,4]])#列表
>>> A
matrix([[1, 2],
[3, 4]])
>>> B=np.mat("3,2,1;7,6,8")#数字字符串
>>> B
matrix([[3, 2, 1],
[7, 6, 8]])
>>> C = np.mat(((1,2),(3,4)))#元组
>>> C
matrix([[1, 2],
[3, 4]])
2. 从numpy数组创建矩阵
>>> a=np.array([[1,2],[3,4]])
>>> np.mat(a)
matrix([[1, 2],
[3, 4]])
3. 从已有矩阵中通过bmat函数创建
>>> A = np.mat(np.eye(2))
>>> A
matrix([[1., 0.],
[0., 1.]])
>>> B = A*2
>>> B
matrix([[2., 0.],
[0., 2.]])
>>> np.bmat("A B; B A")
matrix([[1., 0., 2., 0.],
[0., 1., 0., 2.],
[2., 0., 1., 0.],
[0., 2., 0., 1.]])
>>> A = np.mat(np.random.rand(3,2))
>>> A
matrix([[0.88548289, 0.32510849],
[0.4915165 , 0.52896972],
[0.32423831, 0.59844983]])
>>> A.T
matrix([[0.88548289, 0.4915165 , 0.32423831],
[0.32510849, 0.52896972, 0.59844983]])
>>> A = np.random.randn(3,3)
>>> A = np.mat(A)
>>> A
matrix([[ 0.62735613, -0.58155189, 0.33313612],
[-0.56945614, 1.02485754, 0.16677127],
[-0.54911992, -0.80337017, 1.68631462]])
>>> np.linalg.det(A) #需用到线性代数子库linalg
1.0029561427173685
>>> A=np.mat([[1,2,3],[2,4,6]])
>>> np.linalg.matrix_rank(A)
1
>>> B=np.mat([[1,2,3],[2,4,5]])
>>> np.linalg.matrix_rank(B)
2
矩阵的的逆矩阵
>>> A = np.random.randn(3,3)
>>> A= np.mat(A)
>>> A
matrix([[-0.01513886, 0.90067747, -0.97224512],
[-0.49613961, -0.96700178, -1.28164602],
[-1.16368445, -0.3528249 , -1.0021803 ]])
>>> A.I #前提是det(A)不为0
matrix([[ 0.28535139, 0.68764824, -1.1562322 ],
[ 0.54883314, -0.61618317, 0.25557115],
[-0.52455728, -0.58153289, 0.25476211]])
>>> A**(-1)
matrix([[ 0.28535139, 0.68764824, -1.1562322 ],
[ 0.54883314, -0.61618317, 0.25557115],
[-0.52455728, -0.58153289, 0.25476211]])
>>> A*A.I #由于精度问题,结果近似为单位矩阵
matrix([[ 1.00000000e+00, -8.96322141e-17, -1.70539774e-18],
[-1.51263266e-16, 1.00000000e+00, 2.74501368e-17],
[-2.67373528e-17, 1.46098928e-17, 1.00000000e+00]])
>>> A = np.mat("1,3;5,2")
>>>B=np.mat([[11], [16]])
>>> X=np.linalg.solve(A,B) #det(A)!=0时,X=A.I * B
>>> X
matrix([[2.],
[3.]])
>>> A*X == B #验证
matrix([[ True],
[ True]])
如果向量v与变换A满足Av=λv,则称向量v是变换A的一个特征向量,λ是相应的特征值。
>>> A = np.mat(np.random.randn(3,3))
>>> A
matrix([[ 0.42631062, 2.64734627, -2.11149817],
[-1.23947749, -0.63390069, 0.42119854],
[ 0.61590965, -0.23284082, 0.47037727]])
>>> eigenvalues,eigvector=np.linalg.eig(A)
>>> eigenvalues #特征值为数组
array([0.04862084+2.07208694j, 0.04862084-2.07208694j,
0.16554552+0.j ])
>>> eigvector #特征向量为矩阵
matrix([[ 0.82817694+0.j , 0.82817694-0.j ,
-0.1429864 +0.j ],
[-0.19877709+0.45047014j, -0.19877709-0.45047014j,
0.62570857+0.j ],
[-0.10108359-0.2479302j , -0.10108359+0.2479302j ,
0.76684006+0.j ]])
其中U是m×m阶酉矩阵;Σ是半正定m×n阶对角矩阵;而V*,即V的共轭转置,是n×n阶酉矩阵。这样的分解就称作M的奇异值分解。Σ对角线上的元素Σi,其中Σi即为M的奇异值。 常见的做法是为了奇异值由大而小排列。如此Σ便能由M唯一确定了。(虽然U和V仍然不能确定)
>>> A =np.mat("2 6 14;8 3 2")
>>> A
matrix([[ 2, 6, 14],
[ 8, 3, 2]])
>>> U,sigma,V=np.linalg.svd(A,full_matrices=False)
>>> U
matrix([[-0.94566125, -0.3251535 ],
[-0.3251535 , 0.94566125]])
>>> sigma
array([16.04113167, 7.46204361])
>>> V
matrix([[-0.28006444, -0.41452362, -0.86587186],
[ 0.92668756, 0.11874264, -0.35658147]])
>>> U*np.diag(sigma)*V #验证。结果等于A(有误差)
matrix([[ 2., 6., 14.],
[ 8., 3., 2.]])
本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!