一、 在Jupyter中进行Python矩阵基本运算
实验环境:
Anaconda + python3.7 + jupyter
1.python矩阵基本操作
- 矩阵的加减,行列转换
代码如下:
import numpy as np
a = np.mat([[1, 2, 3], [4, 5, 6]])
a.shape
a.T
b = np.array([[1, 2, 3], [4, 5, 6]])
a + b
a - b
2.python矩阵乘法
注:
矩阵之间的相乘和数的相乘不一样。
- 1.实现数乘
A = np.array([[1, 2, 3], [4, 5, 6]])
B = A.T
2 * A
结果如下:
- 2.矩阵相乘
完整代码如下:
A = np.array([[1, 2, 3], [4, 5, 6]])
B = A.T
2 * A
np.dot(A, B)
np.dot( B, A)
C = np.array([[1, 2], [1, 3]])
np.dot(np.dot(A, B), C)
np.dot(A, np.dot(B, C))
A = B - 1
np.dot(A+B, C)
np.dot(A, C) + np.dot(B, C)
2*(np.dot(A, C