Matrix是Array的一個(gè)小的分支,包含于Array。所以matrix 擁有array的所有特性。
但在數(shù)組乘和矩陣乘時(shí),兩者各有不同,如果a和b是兩個(gè)matrices,那么a*b,就是矩陣積
如果a,b是數(shù)組的話,則a*b是數(shù)組的運(yùn)算
1.對(duì)數(shù)組的操作
>>> import numpy as np
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]]) >>> a array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> b=a.copy() >>> b array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> a+b#多維數(shù)組的加減,按對(duì)應(yīng)位置操作 array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]]) >>> a*3#多維數(shù)組乘常數(shù),則對(duì)數(shù)組中每一個(gè)元素乘該常數(shù) array([[ 3, 6, 9], [12, 15, 18], [21, 24, 27]]) >>> np.dot(a,b)#數(shù)組的點(diǎn)乘運(yùn)算通過(guò)np.dot(a,b)來(lái)實(shí)現(xiàn),相當(dāng)于矩陣乘 array([[ 30, 36, 42], [ 66, 81, 96], [102, 126, 150]]) >>> c=np.array([1,2,3])#構(gòu)造一行三列的數(shù)組 >>> c array([1, 2, 3]) >>> c*a#c為一行三列,放于數(shù)組a之前,則對(duì)數(shù)組a中每行對(duì)應(yīng)位置相乘 array([[ 1, 4, 9], [ 4, 10, 18], [ 7, 16, 27]]) >>> a*c#c為一行三列,放于數(shù)組a之后,依舊是對(duì)數(shù)組a中每行對(duì)應(yīng)位置相乘 array([[ 1, 4, 9], [ 4, 10, 18], [ 7, 16, 27]]) >>> #如果想要矩陣運(yùn)算,則需要np.dot()函數(shù) >>> np.dot(c,a)#c為一行三列,放于數(shù)組a之前,按正常矩陣方式運(yùn)算 array([30, 36, 42]) >>> np.dot(a,c)#c為一行三列,放于數(shù)組a之后,相當(dāng)于矩陣a乘以3行一列的c矩陣,返回結(jié)果值不變,格式為1行3列 array([14, 32, 50]) >>> #將c改為多行一列的形式 >>> d=c.reshape(3,1) >>> d array([[1], [2], [3]]) >>> # >>> np.dot(a,d)#值與np.dot(a,c)一致,但格式以改變?yōu)?行1列 array([[14], [32], [50]]) >>> a*a#數(shù)組使用*的運(yùn)算其結(jié)果屬于數(shù)組運(yùn)算,對(duì)應(yīng)位置元素之間的運(yùn)算 array([[ 1, 4, 9], [16, 25, 36], [49, 64, 81]]) >>> #但是不能更改a,d點(diǎn)乘的位置,不符合矩陣運(yùn)算格式 >>> np.dot(d,a) Traceback (most recent call last): File "", line 1, in np.dot(d,a) ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)
對(duì)于數(shù)組的轉(zhuǎn)置,求逆,求跡運(yùn)算請(qǐng)參考上篇文章
2.對(duì)矩陣的操作
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]]) >>> a=np.mat(a) >>> a matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> b=a >>> b matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> a+b#矩陣的加減運(yùn)算和數(shù)組運(yùn)算一致 matrix([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]]) >>> a-b matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) >>> a*b#矩陣的乘用*即可表示 matrix([[ 30, 36, 42], [ 66, 81, 96], [102, 126, 150]]) >>> np.dot(a,b)#與*一致 matrix([[ 30, 36, 42], [ 66, 81, 96], [102, 126, 150]]) >>> b*a matrix([[ 30, 36, 42], [ 66, 81, 96], [102, 126, 150]]) >>> np.dot(b,a) matrix([[ 30, 36, 42], [ 66, 81, 96], [102, 126, 150]]) >>> c=np.array([1,2,3])#構(gòu)造一行三列數(shù)組 >>> c array([1, 2, 3]) >>> c*a#矩陣運(yùn)算 matrix([[30, 36, 42]]) >>> a*c#不合矩陣規(guī)則 Traceback (most recent call last): File "", line 1, in a*c File "F:\python3\anzhuang\lib\site-packages\numpy\matrixlib\defmatrix.py", line 309, in __mul__ return N.dot(self, asmatrix(other)) ValueError: shapes (3,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0) >>> np.dot(c,a)#和矩陣運(yùn)算一致 matrix([[30, 36, 42]]) >>> np.dot(a,c)#自動(dòng)將a轉(zhuǎn)換成3行1列參與運(yùn)算,返回結(jié)果格式已經(jīng)變?yōu)?行3列而非3行一列的矩陣 matrix([[14, 32, 50]]) >>> c=c.reshape(3,1) >>> c array([[1], [2], [3]]) >>> a*c#和矩陣運(yùn)算一致 matrix([[14], [32], [50]]) >>> c*a#不合矩陣運(yùn)算格式 Traceback (most recent call last): File " ", line 1, in c*a ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)
矩陣運(yùn)算的另一個(gè)好處就是方便于求轉(zhuǎn)置,求逆,求跡
>>> a matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> a.T matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]]) >>> a.H#共軛轉(zhuǎn)置 matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]]) >>> b=np.eye(3)*3 >>> b array([[3., 0., 0.], [0., 3., 0.], [0., 0., 3.]]) >>> b=np.mat(b) >>> b.I#求逆運(yùn)算 matrix([[0.33333333, 0. , 0. ], [0. , 0.33333333, 0. ], [0. , 0. , 0.33333333]]) >>> np.trace(b)#求跡運(yùn)算 9.0
以上所述是小編給大家介紹的python中數(shù)組和矩陣乘法及使用總結(jié)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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