Numpy矩阵基础操作命令

本文详细介绍了使用NumPy进行矩阵创建、加减乘除运算、求和、均值、极值及条件变化等操作的方法。从arange和reshape创建矩阵到矩阵间的复杂运算,再到利用np.where()对矩阵元素进行条件性修改,全面覆盖了NumPy在矩阵处理上的强大功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建矩阵

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]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值