#numpy ##数组间运算

数组间运算

数组和数字是可以直接进行计算的
数组和数组需要满足广播机制

1 数组与数的运算

1.1创建一个数组
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
[[  0  50 100 150]  [200 250 300 350]  [400 450 500 550]  [600 650 700 750]  [800 850 900 950]]
1.2数组与数加法运算
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b + 8
print(c)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\pythonProject1\main.py 
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[[  1  51 101 151]
 [201 251 301 351]
 [401 451 501 551]
 [601 651 701 751]
 [801 851 901 951]]

Process finished with exit code 0
1.3数组与数减法运算
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b - 1
print(c)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\pythonProject1\main.py 
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[[ -1  49  99 149]
 [199 249 299 349]
 [399 449 499 549]
 [599 649 699 749]
 [799 849 899 949]]

Process finished with exit code 0
1.4数组与数除法运算
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b / 10
print(c)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\pythonProject1\main.py 
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[[ 0.  5. 10. 15.]
 [20. 25. 30. 35.]
 [40. 45. 50. 55.]
 [60. 65. 70. 75.]
 [80. 85. 90. 95.]]

Process finished with exit code 0
1.5数组与数乘法运算
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b * 10
print(c)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\pythonProject1\main.py 
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[[   0  500 1000 1500]
 [2000 2500 3000 3500]
 [4000 4500 5000 5500]
 [6000 6500 7000 7500]
 [8000 8500 9000 9500]]

Process finished with exit code 0

2 数组与数组的运算

数组和数字是可以直接进行计算的
数组和数组需要满足广播机制

2.1可以计算
Image (3d array):  256 x 256 x 3
Scale (1d array):              3
Result (3d array): 256 x 256 x 3

A      (4d array):  9 x 1 x 7 x 1
B      (3d array):      8 x 1 x 5
Result (4d array):  9 x 8 x 7 x 5

A      (2d array):  5 x 4
B      (1d array):      1
Result (2d array):  5 x 4

A      (3d array):  15 x 3 x 5
B      (3d array):  15 x 1 x 1
Result (3d array):  15 x 3 x 5
2.2不可以计算
A  (1d array): 10
B  (1d array): 12
A  (2d array):      2 x 1
B  (3d array):  8 x 4 x 3 
2.3 广播机制

执行 broadcast 的前提在于,两个 ndarray 执行的是element-wise的运算,Broadcast机制的功能是为了方便不同形状的ndarray(numpy库的核心数据结构)进行数学运算。

当操作两个数组时,numpy会逐个比较它们的shape(构成的元组tuple),只有在下述情况下,两个数组才能够进行数组与数组的运算

  • 维度相等
  • shape(其中相对应的一个地方为1

3 矩阵运算

  • np.matmul数组相乘
  • np.dot点乘
3.1点乘
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b[0]
print(c)
d = np.dot(b,c)
print(d)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\pythonProject1\main.py 
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[  0  50 100 150]
[ 35000  95000 155000 215000 275000]

Process finished with exit code 0
3.2 数组相乘
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b[0]
print(c)
d = np.matmul(b,c)
print(d)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\pythonProject1\main.py 
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[  0  50 100 150]
[ 35000  95000 155000 215000 275000]

Process finished with exit code 0
3.3 np.matmul数组相乘 np.dot点乘的区别
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b[0]
print(c)
d = np.dot(b,3)
print(d)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\pythonProject1\main.py 
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[  0  50 100 150]
[[   0  150  300  450]
 [ 600  750  900 1050]
 [1200 1350 1500 1650]
 [1800 1950 2100 2250]
 [2400 2550 2700 2850]]

Process finished with exit code 0

np.matmul不能和数字相乘,否则会报错

import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b[0]
print(c)
d = np.matmul(b,3) 
print(d)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\pythonProject1\main.py 
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[  0  50 100 150]
Traceback (most recent call last):
  File "C:\Users\Windows11\Desktop\pythonProject1\main.py", line 7, in <module>
    d = np.matmul(b,3)
ValueError: matmul: Input operand 1 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

Process finished with exit code 1

问题出在d = np.matmul(b,3) 这行代码

np.matmul
np.dot
注意:二者都是矩阵乘法。
np.matmul中禁止矩阵与标量的乘法。
在矢量乘矢量的內积运算中,np.matmul与np.dot没有区别。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值