1. 同線性代數中矩陣乘法的定義: np.dot()
np.dot(A, B):對于二維矩陣,計算真正意義上的矩陣乘積,同線性代數中矩陣乘法的定義。對于一維矩陣,計算兩者的內積。見如下Python代碼:
import numpy as np # 2-D array: 2 x 3 two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]]) # 2-D array: 3 x 2 two_dim_matrix_two = np.array([[1, 2], [3, 4], [5, 6]]) two_multi_res = np.dot(two_dim_matrix_one, two_dim_matrix_two) print('two_multi_res: %s' %(two_multi_res)) # 1-D array one_dim_vec_one = np.array([1, 2, 3]) one_dim_vec_two = np.array([4, 5, 6]) one_result_res = np.dot(one_dim_vec_one, one_dim_vec_two) print('one_result_res: %s' %(one_result_res))
結果如下:
two_multi_res: [[22 28] [49 64]] one_result_res: 32
2. 對應元素相乘 element-wise product: np.multiply(), 或 *
在Python中,實現對應元素相乘,有2種方式,一個是np.multiply(),另外一個是*。見如下Python代碼:
import numpy as np # 2-D array: 2 x 3 two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]]) another_two_dim_matrix_one = np.array([[7, 8, 9], [4, 7, 1]]) # 對應元素相乘 element-wise product element_wise = two_dim_matrix_one * another_two_dim_matrix_one print('element wise product: %s' %(element_wise)) # 對應元素相乘 element-wise product element_wise_2 = np.multiply(two_dim_matrix_one, another_two_dim_matrix_one) print('element wise product: %s' % (element_wise_2))
結果如下:
element wise product: [[ 7 16 27] [16 35 6]] element wise product: [[ 7 16 27] [16 35 6]]
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
