np.dot()函數(shù)的用法詳解
基本簡(jiǎn)介
dot函數(shù)為numpy庫(kù)下的一個(gè)函數(shù),主要用于矩陣的乘法運(yùn)算,其中包括:向量?jī)?nèi)積、多維矩陣乘法和矩陣與向量的乘法。
1. 向量?jī)?nèi)積
向量其實(shí)是一維的矩陣,兩個(gè)向量進(jìn)行內(nèi)積運(yùn)算時(shí),需要保證兩個(gè)向量包含的元素個(gè)數(shù)是相同的。
例1:
import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7]) y = np.array([2, 3, 4, 5, 6, 7, 8]) result = np.dot(x, y) print(result)
輸出結(jié)果:
168
計(jì)算過(guò)程就是將向量中對(duì)應(yīng)元素相乘,再相加所得。即普通的向量乘法運(yùn)算。
2. 矩陣乘法運(yùn)算
兩個(gè)矩陣(x, y)如果可以進(jìn)行乘法運(yùn)算,需要滿足以下條件:
x為 m×n 階矩陣,y為 n×p 階矩陣,
則相乘的結(jié)果 result 為 m×p 階矩陣。
例2:
import numpy as np
x = np.array([[1, 2, 3],
[3, 4, 4]])
y = np.array([[0, 1, 1, 1],
[1, 2, 0, 1],
[0, 0, 2, 1]])
result = np.dot(x, y)
print(result)
print("x階數(shù):" + str(x.shape))
print("y階數(shù):" + str(y.shape))
print("result階數(shù):" + str(result.shape))
結(jié)果為:
[[ 2 5 7 6]
[ 4 11 11 11]]
x階數(shù):(2, 3)
y階數(shù):(3, 4)
result階數(shù):(2, 4)
dot(x, y)不等于dot(y, x),矩陣乘法不滿足交換律
例3:
import numpy as np
x = np.array([[1, 2],
[3, 4]])
y = np.array([[2, 2],
[1, 2]])
result1 = np.dot(x, y)
result2 = np.dot(y, x)
print("result1 = " + str(result1))
print("result2 = " + str(result2))
結(jié)果為:
result1 = [[ 4 6]
[10 14]]
result2 = [[ 8 12]
[ 7 10]]
如果不滿足運(yùn)算前提,都不可以運(yùn)算。例2的dot(y,x)不滿足運(yùn)算條件,因此運(yùn)算會(huì)報(bào)錯(cuò)。
例4:
import numpy as np x = np.array([[1, 2, 3], [3, 4, 4]]) y = np.array([[0, 1, 1, 1], [1, 2, 0, 1], [0, 0, 2, 1]]) result = np.dot(y, x) print(result)
結(jié)果為:
Traceback (most recent call last):
File "numpy1.py", line 96, in <module>
result = np.dot(y,x)
File "<__array_function__ internals>", line 6, in dot
ValueError: shapes (3,4) and (2,3) not aligned: 4 (dim 1) != 2 (dim 0)
3. 矩陣與向量乘法
矩陣x為m×n階,向量y為n階向量,則矩陣x和向量y可以進(jìn)行乘法運(yùn)算,結(jié)果為m階向量。進(jìn)行運(yùn)算時(shí),會(huì)首先將后面一項(xiàng)進(jìn)行自動(dòng)轉(zhuǎn)置操作,之后再進(jìn)行乘法運(yùn)算。
例5:
import numpy as np
x = np.array([[1, 2, 3],
[3, 4, 4]])
y = np.array([1, 2, 3])
result = np.dot(x, y)
print(result)
print("x階數(shù):" + str(x.shape))
print("y階數(shù):" + str(y.shape))
print("result階數(shù):" + str(result.shape))
結(jié)果為:
[14 23]
x階數(shù):(2, 3)
y階數(shù):(3,)
result階數(shù):(2,)
例6:仍然不滿足交換律
import numpy as np
x = np.array([[1, 2, 3],
[3, 4, 4],
[0, 1, 1]])
y = np.array([1, 2, 3])
result1 = np.dot(x, y) # 1×1 + 2×2 + 3×3 = 14(result1的第一個(gè)元素)
result2 = np.dot(y, x) # 1×1 + 2×3 + 3×0 = 7 (result2的第一個(gè)元素)
print("result1 = " + str(result1))
print("result2 = " + str(result2))
結(jié)果為:
result1 = [14 23 5]
result2 = [ 7 13 14]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch簡(jiǎn)單實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)功能
這篇文章主要介紹了pytorch簡(jiǎn)單實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
使用Python requests庫(kù)發(fā)送JSON數(shù)據(jù)的POST請(qǐng)求步驟
在Python這個(gè)強(qiáng)大的編程語(yǔ)言中,requests庫(kù)是一個(gè)廣泛使用且功能強(qiáng)大的HTTP請(qǐng)求庫(kù),發(fā)送POST請(qǐng)求并附帶JSON數(shù)據(jù)是一個(gè)非常常見的需求,本文給大家介紹了如何用Python的requests庫(kù)發(fā)送JSON數(shù)據(jù)的POST請(qǐng)求,需要的朋友可以參考下2024-06-06
Python如何通過(guò)subprocess調(diào)用adb命令詳解
python可以說(shuō)是寫一些小腳本的利器語(yǔ)法簡(jiǎn)單,做為最著名的就“膠水語(yǔ)言”用它來(lái)寫一些命令腳本非常的方便。下面這篇文章主要給大家介紹了關(guān)于Python如何通過(guò)subprocess調(diào)用adb命令的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
基于python實(shí)現(xiàn)的百度新歌榜、熱歌榜下載器(附代碼)
這篇文章主要介紹了基于python實(shí)現(xiàn)的百度新歌榜、熱歌榜下載器(附代碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python數(shù)據(jù)結(jié)構(gòu)與算法之使用隊(duì)列解決小貓釣魚問(wèn)題
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之使用隊(duì)列解決小貓釣魚問(wèn)題,結(jié)合實(shí)例形式分析了Python使用隊(duì)列實(shí)現(xiàn)小貓釣魚游戲的算法操作技巧,代碼中備有較為詳盡的注釋便于讀者理解,需要的朋友可以參考下2017-12-12

