python的幾種矩陣相乘的公式詳解
1. 同線性代數(shù)中矩陣乘法的定義: np.dot()
np.dot(A, B):對于二維矩陣,計算真正意義上的矩陣乘積,同線性代數(shù)中矩陣乘法的定義。對于一維矩陣,計算兩者的內(nèi)積。見如下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))
結(jié)果如下:
two_multi_res: [[22 28] [49 64]] one_result_res: 32
2. 對應(yīng)元素相乘 element-wise product: np.multiply(), 或 *
在Python中,實現(xiàn)對應(yīng)元素相乘,有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]]) # 對應(yīng)元素相乘 element-wise product element_wise = two_dim_matrix_one * another_two_dim_matrix_one print('element wise product: %s' %(element_wise)) # 對應(yīng)元素相乘 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))
結(jié)果如下:
element wise product: [[ 7 16 27] [16 35 6]] element wise product: [[ 7 16 27] [16 35 6]]
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)對相同數(shù)據(jù)分箱的小技巧分享
這篇文章主要給大家介紹了關(guān)于Python實現(xiàn)對相同數(shù)據(jù)分箱的小技巧,文中通過實例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友可以參考下2022-01-01解決python ThreadPoolExecutor 線程池中的異常捕獲問題
這篇文章主要介紹了解決python ThreadPoolExecutor 線程池中的異常捕獲問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04pyqt 實現(xiàn)為長內(nèi)容添加滑輪 scrollArea
今天小編就為大家分享一篇pyqt 實現(xiàn)為長內(nèi)容添加滑輪 scrollArea,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06利用Python2下載單張圖片與爬取網(wǎng)頁圖片實例代碼
這篇文章主要給大家介紹了關(guān)于利用Python2下載單張圖片與爬取網(wǎng)頁圖片的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-12-12Python Threading 線程/互斥鎖/死鎖/GIL鎖
這篇文章主要介紹了Python Threading 線程/互斥鎖/死鎖/GIL鎖的相關(guān)知識,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07關(guān)于Qt6中QtMultimedia多媒體模塊的重大改變分析
如果您一直在 Qt 5 中使用 Qt Multimedia,則需要對您的實現(xiàn)進行更改。這篇博文將嘗試引導您完成最大的變化,同時查看 API 和內(nèi)部結(jié)構(gòu)2021-09-09