当前位置: 代码迷 >> 综合 >> numpy 中星乘、np.multiply、np.dot 、np.matmul 等乘法相关方法的使用实例
  详细解决方案

numpy 中星乘、np.multiply、np.dot 、np.matmul 等乘法相关方法的使用实例

热度:2   发布时间:2024-01-14 06:50:01.0
版本
>>> np.__version__
'1.16.5'
功能说明
  • 星乘(*):数组中对应位置元素相乘,功能同 np.multiply()
    • 官方文档:参考 np.multiply()
  • np.multiply():数组中对应位置元素相乘,功能同星乘(*)
    • 官方文档:https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html
  • np.dot():两个数组的点积,矩阵乘
    • 官方文档:https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html?highlight=dot#numpy.dot
    • 一维、二维等同于 np.matmul()
  • np.matmul()
    • 官方文档:https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html?highlight=matmul#numpy.matmul
    • 不支持标量,一维、二维等同于 np.dot()
实例
标量
data1 = 2
data2 = 3# 星乘(*)示例
>>> data1 * data2
6# np.multiply() 示例
>>> np.multiply(data1, data2)
6# np.dot() 示例
>>> np.dot(data1, data2)
6# narray.dot() 示例 
>>> data1.dot(data2)    # 标量没有 dot 属性
Traceback (most recent call last):File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'dot'# np.matmul() 示例
>>> np.matmul(data1,data2)  # 不支持标量运算
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

一维数组

>>> data1 = np.random.randint(0,10,1)
>>> data1
array([2])
>>> data2 = np.random.randint(0,10,1)
>>> data2
array([1])# 星乘(*)示例
>>> data1 * data2
array([2])# np.multiply() 示例
>>> np.multiply(data1, data2)
array([2])# np.dot() 示例
>>> np.dot(data1, data2)    # 两个数组都是一维时,相当于相邻的内积
2# narray.dot() 示例 
>>> data1.dot(data2)    # 两个数组都是一维时,相当于相邻的内积
2# np.matmul() 示例
>>> np.matmul(data1,data2)
2

二维数组

>>> data1 = np.random.randint(0,10,10).reshape(5,2)
>>> data1
array([[6, 9],[5, 1],[9, 1],[8, 6],[7, 0]])
>>> data2 = np.random.randint(0,10,10).reshape(5,2)
>>> data2
array([[9, 0],[4, 6],[6, 6],[4, 5],[9, 9]])
>>> data3 = np.random.randint(0,10,6).reshape(2,3)
>>> data3
array([[5, 4, 5],[4, 2, 7]])# 星乘(*)示例
>>> data1*data2
array([[54,  0],[20,  6],[54,  6],[32, 30],[63,  0]])
>>> data1*data3 # 不满足对应位置元素相乘的要求 
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (5,2) (2,3)# np.multiply() 示例
>>> np.multiply(data1,data1)
array([[54,  0],[20,  6],[54,  6],[32, 30],[63,  0]])
>>> np.multiply(data1,data2)    # 不满足对应位置元素相乘的要求 
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (5,2) (2,3)# np.dot() 示例
>>> np.dot(data1,data2)     # 不满足数组点乘的要求
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: shapes (5,2) and (5,2) not aligned: 2 (dim 1) != 5 (dim 0)
>>> np.dot(data1,data2)  ## 两个数组都是 2 维时,相当于矩阵乘,但更推荐使用 np.matmul 或 data1@data3
array([[66, 42, 93],[29, 22, 32],[49, 38, 52],[64, 44, 82],[35, 28, 35]])# narray.dot() 示例
>>> data1.dot(data2)    # 不满足数组点乘的要求
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: shapes (5,2) and (5,2) not aligned: 2 (dim 1) != 5 (dim 0)
>>> data1.dot(data3)    # 两个数组都是 2 维时,相当于矩阵乘,但更推荐使用 np.matmul 或 data1@data3
array([[66, 42, 93],[29, 22, 32],[49, 38, 52],[64, 44, 82],[35, 28, 35]])# np.matmul() 示例
>>> np.matmul(data1,data2)   # 不满足矩阵乘的要求 
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 5 is different from 2)
>>> np.matmul(data1,data3)
array([[66, 42, 93],[29, 22, 32],[49, 38, 52],[64, 44, 82],[35, 28, 35]])

三维数组

>>> data1 = np.random.randint(0,10,30).reshape(3,2,5)
>>> data1
array([[[5, 4, 8, 8, 7],[4, 3, 5, 9, 1]],[[2, 7, 1, 0, 7],[6, 3, 6, 7, 2]],[[0, 9, 3, 5, 8],[9, 2, 9, 6, 6]]])
>>> data2 = np.random.randint(0,10,15).reshape(3,1,5)
>>> data2
array([[[7, 9, 9, 6, 3]],[[3, 5, 2, 5, 7]],[[8, 3, 4, 7, 9]]])
>>> data3 = np.random.randint(0,10,30).reshape(3,5,2)
>>> data3
array([[[2, 9],[5, 6],[2, 5],[1, 7],[5, 8]],[[7, 6],[9, 4],[5, 1],[1, 7],[8, 1]],[[3, 0],[0, 0],[9, 5],[3, 4],[9, 4]]])# 星乘(*)示例
>>> data1*data2
array([[[35, 36, 72, 48, 21],[28, 27, 45, 54,  3]],[[ 6, 35,  2,  0, 49],[18, 15, 12, 35, 14]],[[ 0, 27, 12, 35, 72],[72,  6, 36, 42, 54]]])
>>> data1*data3     # 不满足对应位置元素相乘的要求 
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (3,2,5) (3,5,2)# np.multiply() 示例
>>> np.multiply(data1,data2)
array([[[35, 36, 72, 48, 21],[28, 27, 45, 54,  3]],[[ 6, 35,  2,  0, 49],[18, 15, 12, 35, 14]],[[ 0, 27, 12, 35, 72],[72,  6, 36, 42, 54]]])>>> np.multiply(data1, data3)   # 不满足对应位置元素相乘的要求 
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (3,2,5) (3,5,2)# np.dot() 示例
>>> np.dot(data1, data2)    # 不满足数组点乘的要求
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: shapes (3,2,5) and (3,1,5) not aligned: 5 (dim 2) != 1 (dim 1)
>>> data1.shape
(3, 2, 5)
>>> data2.shape
(3, 1, 5)
>>> data3.shape
(3, 5, 2)
>>> data = np.dot(data1, data3) # 如果两个数组超过 2 维时,结果时第一个数组的最后一维和第二个数组倒数第二维的和积
>>> data.shape
(3, 2, 3, 2)
>>> data
array([[[[ 89, 221],[175, 117],[174, 100]],[[ 47, 150],[ 97, 105],[ 93,  65]]],[[[ 76, 121],[138,  48],[ 78,  33]],[[ 56, 167],[122, 105],[111,  66]]],[[[ 96, 168],[165,  82],[114,  67]],[[ 82, 228],[180, 119],[180,  93]]]])# narray.dot() 示例
>>> data1.dot(data2)    # 不满足矩阵乘的要求
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: shapes (3,2,5) and (3,1,5) not aligned: 5 (dim 2) != 1 (dim 1)
>>> data1.dot(data3)
array([[[[ 89, 221],[175, 117],[174, 100]],[[ 47, 150],[ 97, 105],[ 93,  65]]],[[[ 76, 121],[138,  48],[ 78,  33]],[[ 56, 167],[122, 105],[111,  66]]],[[[ 96, 168],[165,  82],[114,  67]],[[ 82, 228],[180, 119],[180,  93]]]])# np.matmul() 示例
>>> np.matmul(data1,data2)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 5)
>>> data1.shape
(3, 2, 5)
>>> data2.shape
(3, 1, 5)
>>> data3.shape
(3, 5, 2)
>>> data = np.matmul(data1,data3)
>>> data.shape
(3, 2, 2)
>>> data
array([[[ 89, 221],[ 47, 150]],[[138,  48],[122, 105]],[[114,  67],[180,  93]]])