python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解
為了區(qū)分三種乘法運(yùn)算的規(guī)則,具體分析如下:
import numpy as np1. np.multiply()函數(shù)
函數(shù)作用
數(shù)組和矩陣對(duì)應(yīng)位置相乘,輸出與相乘數(shù)組/矩陣的大小一致
1.1數(shù)組場(chǎng)景A = np.arange(1,5).reshape(2,2)A
array([[1, 2], [3, 4]])
B = np.arange(0,4).reshape(2,2)B
array([[0, 1], [2, 3]])
np.multiply(A,B) #數(shù)組對(duì)應(yīng)元素位置相乘
array([[ 0, 2], [ 6, 12]])
1.2 矩陣場(chǎng)景np.multiply(np.mat(A),np.mat(B)) #矩陣對(duì)應(yīng)元素位置相乘,利用np.mat()將數(shù)組轉(zhuǎn)換為矩陣
matrix([[ 0, 2],[ 6, 12]])
np.sum(np.multiply(np.mat(A),np.mat(B))) #輸出為標(biāo)量
20
2. np.dot()函數(shù)函數(shù)作用
對(duì)于秩為1的數(shù)組,執(zhí)行對(duì)應(yīng)位置相乘,然后再相加;
對(duì)于秩不為1的二維數(shù)組,執(zhí)行矩陣乘法運(yùn)算;超過(guò)二維的可以參考numpy庫(kù)介紹。
2.1 數(shù)組場(chǎng)景2.1.1 數(shù)組秩不為1的場(chǎng)景
A = np.arange(1,5).reshape(2,2)A
array([[1, 2], [3, 4]])
B = np.arange(0,4).reshape(2,2)B
array([[0, 1], [2, 3]])
np.dot(A,B) #對(duì)數(shù)組執(zhí)行矩陣相乘運(yùn)算
array([[ 4, 7], [ 8, 15]])
2.1.2 數(shù)組秩為1的場(chǎng)景
C = np.arange(1,4)C
array([1, 2, 3])
D = np.arange(0,3)D
array([0, 1, 2])
np.dot(C,D) #對(duì)應(yīng)位置相乘,再求和
8
2.2 矩陣場(chǎng)景np.dot(np.mat(A),np.mat(B)) #執(zhí)行矩陣乘法運(yùn)算
matrix([[ 4, 7],[ 8, 15]])
3. 星號(hào)(*)乘法運(yùn)算作用
對(duì)數(shù)組執(zhí)行對(duì)應(yīng)位置相乘
對(duì)矩陣執(zhí)行矩陣乘法運(yùn)算
3.1 數(shù)組場(chǎng)景A = np.arange(1,5).reshape(2,2)A
array([[1, 2], [3, 4]])
B = np.arange(0,4).reshape(2,2)B
array([[0, 1], [2, 3]])
A*B #對(duì)應(yīng)位置點(diǎn)乘
array([[ 0, 2], [ 6, 12]])
3.2矩陣場(chǎng)景(np.mat(A))*(np.mat(B)) #執(zhí)行矩陣運(yùn)算
matrix([[ 4, 7],[ 8, 15]])
到此這篇關(guān)于python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解的文章就介紹到這了,更多相關(guān)python np.multiply()、np.dot()和星號(hào)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Android 7.0 運(yùn)行時(shí)權(quán)限彈窗問(wèn)題的解決2. java實(shí)現(xiàn)圖形化界面計(jì)算器3. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟4. IDEA的Mybatis Generator駝峰配置問(wèn)題5. ASP.NET MVC解決上傳圖片臟數(shù)據(jù)的方法6. 如何利用python和DOS獲取wifi密碼7. Thinkphp3.2.3反序列化漏洞實(shí)例分析8. python Xpath語(yǔ)法的使用9. 原生js XMLhttprequest請(qǐng)求onreadystatechange執(zhí)行兩次的解決10. python 批量將PPT導(dǎo)出成圖片集的案例
