创建矩阵
1: arange+reshape,arange()按照指定步长递增生成
>>> mat = np.arange(0,18,2)
>>> mat = mat.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
2: array+reshape
>>> mat = np.array([1,3,4,6])
>>> mat = mat.reshape(2,2)
[[1 3]
[4 6]]
3: empty()生成接近于0的随机数矩阵
>>> mat = np.empty((3,3))
[[0.00000000e+000 0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 3.39917164e-321]
[2.22522596e-306 9.34609789e-307 2.56765117e-312]]
4: ones()生成全1矩阵
>>> mat = np.ones((3,2))
[[1. 1.]
[1. 1.]
[1. 1.]]
5: 生成全0矩阵
>>> mat = np.zeros((3,2))
[[0. 0.]
[0. 0.]
[0. 0.]]
矩阵相加
1: 使用+即可完成矩阵(对应元素处)相加,前提是两个矩阵的行列数相同
>>> matA = np.ones((3,3))
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
>>> matB = np.arange(0,18,2)
>>> matB = matB.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matC = matA+matB
[[ 1. 3. 5.]
[ 7. 9. 11.]
[13. 15. 17.]]
矩阵相减
1: 使用-即可完成矩阵(对应元素处)相减,前提是两个矩阵的行列数相同
>>> matA = np.ones((3,3))
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
>>> matB = np.arange(0,18,2)
>>> matB = matB.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matC = matB-matA
[[-1. 1. 3.]
[ 5. 7. 9.]
[11. 13. 15.]]
矩阵相乘
1: 使用*或者np.dot()即可完成矩阵(对应元素处)相乘,前提是两个矩阵的行列数相同
>>> matA = np.array([1,1,1,2,2,2,3,3,3]).reshape(3,3)
[[1 1 1]
[2 2 2]
[3 3 3]]
>>> matB = np.arange(0,18,2)
>>> matB = matB.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matC = np.multiply(matA,matB)
[[ 0 2 4]
[12 16 20]
[36 42 48]]
>>> matC = matA*matB
[[ 0 2 4]
[12 16 20]
[36 42 48]]
2: 使用np.dot()完成线代中的矩阵乘法,即N*M矩阵乘以M*P矩阵得到N*P的矩阵
>>> matA = np.array([1,2,3,1,2,3]).reshape(2,3)
[[1 2 3]
[1 2 3]]
>>> matB = np.arange(0,12,2)
>>> matB = matB.reshape(3,2)
[[ 0 2]
[ 4 6]
[ 8 10]]
>>> matC = np.dot(matB,matA)
[[ 2 4 6]
[10 20 30]
[18 36 54]]
3: 使用矩阵乘以一个数字,即为各个矩阵元素乘以此数字
>>> matA = np.array([1,2,3,1,2,3]).reshape(2,3)
[[1 2 3]
[1 2 3]]
>>> matC = matA*2
[[2 4 6]
[2 4 6]]
矩阵相除
1: 使用/即可完成矩阵(对应元素处)相除,前提是两个矩阵的行列数相同
>>> matA = np.array([1,1,1,2,2,2,3,3,3]).reshape(3,3)
[[1 1 1]
[2 2 2]
[3 3 3]]
>>> matB = np.arange(0,18,2)
>>> matB = matB.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matC = matB/matA
[[0. 2. 4. ]
[3. 4. 5. ]
[4. 4.66666667 5.33333333]]
2: 可以先对被除的矩阵求倒数然后再乘以另外个矩阵,具体如下
>>> matA = np.array([1,1,1,2,2,2,3,3,3],dtype=np.float64).reshape(3,3)
[[1. 1. 1.]
[2. 2. 2.]
[3. 3. 3.]]
>>> matB = np.arange(0,18,2)
>>> matB = matB.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matC = matB*(matA**-1)
[[0. 2. 4. ]
[3. 4. 5. ]
[4. 4.66666667 5.33333333]]
求和/均值/极值+条件变化
1: 矩阵求和,sum(),行方向求和sum(axis=0),对列求和sum(axis=1)
>>> matA = np.array([1,1,1,2,2,2,3,3,3]).reshape(3,3)
[[1 1 1]
[2 2 2]
[3 3 3]]
>>> num = matA.sum()
18
>>> rowNum = matA.sum(axis=0)
[6 6 6]
>>> colNum = matA.sum(axis=1)
[3 6 9]
2: 求均值,mean(),行方向求均值mean(axis=0),列方向求均值mean(axis=1)
>>> matA = np.array([1,1,1,2,2,2,3,3,3]).reshape(3,3)
[[1 1 1]
[2 2 2]
[3 3 3]]
>>> mean = matA.mean()
2.0
>>> rowMean = matA.mean(axis=0)
[2. 2. 2.]
>>> colMean = matA.mean(axis=1)
[1. 2. 3.]
3: 求最大值max(),最小值min()
>>> matB = np.arange(0,18,2)
>>> matB = matB.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> minNum = matB.min()
0
>>> maxNum = matB.max()
16
4: 求最大值/最小值的索引,argmin()/argmax(),其中可使用axis来指定行/列方向
>>> matB = np.arange(0,18,2)
>>> matB = matB.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> minIndex = matB.argmin(axis=0)
[0 0 0]
>>> maxIndex = matB.argmax(axis=0)
[2 2 2]
5: np.where(),将符合条件的矩阵元素执行指定操作
例: 将matB中大于10的矩阵元素置为-1,其他则不变
>>> matB = np.arange(0,18,2)
>>> matB = matB.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matC = np.where(matB>10,-1,matB)
[[ 0 2 4]
[ 6 8 10]
[-1 -1 -1]]