数组和数字是可以直接进行计算的
数组和数组需要满足广播机制
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]]
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
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
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
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
数组和数字是可以直接进行计算的
数组和数组需要满足广播机制
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
A (1d array): 10
B (1d array): 12
A (2d array): 2 x 1
B (3d array): 8 x 4 x 3
执行 broadcast 的前提在于,两个 ndarray 执行的是element-wise的运算,Broadcast机制的功能是为了方便不同形状的ndarray(numpy库的核心数据结构)进行数学运算。
当操作两个数组时,numpy会逐个比较它们的shape(构成的元组tuple),只有在下述情况下,两个数组才能够进行数组与数组的运算。
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
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
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没有区别。