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

版本
>>> 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]]])
發佈了105 篇原創文章 · 獲贊 28 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章