参考:
https://ailearning.apachecn.org/
使用Jupyter进行练习
import numpy as np
使用 mat
方法将 2 维数组转化为矩阵:
a = np.array([[1,2,4],
[2,5,3],
[7,8,9]])
A = np.mat(a)
A
matrix([[1, 2, 4],
[2, 5, 3],
[7, 8, 9]])
也可以使用 Matlab
的语法传入一个字符串来生成矩阵:
A = np.mat('1,2,4;2,5,3;7,8,9')
A
matrix([[1, 2, 4],
[2, 5, 3],
[7, 8, 9]])
利用分块创造新的矩阵:
a = np.array([[ 1, 2],
[ 3, 4]])
b = np.array([[10,20],
[30,40]])
np.bmat('a,b;b,a')
matrix([[ 1, 2, 10, 20],
[ 3, 4, 30, 40],
[10, 20, 1, 2],
[30, 40, 3, 4]])
矩阵与向量的乘法:
v = np.array([[1], [2], [3]])
v
array([[1],
[2],
[3]])
A * v
matrix([[17],
[21],
[50]])
A.I
表示 A
矩阵的逆矩阵:
A * A.I
matrix([[1.00000000e+00, 5.55111512e-17, 0.00000000e+00],
[0.00000000e+00, 1.00000000e+00, 2.08166817e-17],
[2.22044605e-16, 4.16333634e-17, 1.00000000e+00]])
矩阵指数表示矩阵连乘:
A ** 4
matrix([[ 6497, 9580, 9836],
[ 7138, 10561, 10818],
[18434, 27220, 27945]])