主頁(yè) > 知識(shí)庫(kù) > python numpy中multiply與*及matul 的區(qū)別說(shuō)明

python numpy中multiply與*及matul 的區(qū)別說(shuō)明

熱門(mén)標(biāo)簽:400電話辦理哪種 河北防封卡電銷(xiāo)卡 地圖標(biāo)注線上如何操作 手機(jī)網(wǎng)頁(yè)嵌入地圖標(biāo)注位置 應(yīng)電話機(jī)器人打電話違法嗎 開(kāi)封語(yǔ)音外呼系統(tǒng)代理商 開(kāi)封自動(dòng)外呼系統(tǒng)怎么收費(fèi) 天津電話機(jī)器人公司 電銷(xiāo)機(jī)器人的風(fēng)險(xiǎn)

1、對(duì)于矩陣(matrix)而言

multiply是對(duì)應(yīng)元素相乘,而 * 、np.matmul() 函數(shù) 與 np.dot()函數(shù) 相當(dāng)于矩陣乘法(矢量積),對(duì)應(yīng)的列數(shù)和行數(shù)必須滿足乘法規(guī)則;如果希望以數(shù)量積的方式進(jìn)行,則必須使用 np.multiply 函數(shù),如下所示:

a = np.mat([[1, 2, 3, 4, 5]])
b = np.mat([[1,2,3,4,5]])
c=np.multiply(a,b)
print(c)

結(jié)果是

[[ 1 4 9 16 25]]
a = np.mat([[1, 2, 3, 4, 5]])
b = np.mat([ [1],[2],[3],[4],[5] ] )
d=a*b
print(d) #a是shape(1,5),b是shape(5,1),結(jié)果是一個(gè)實(shí)數(shù)

結(jié)果是

[[55]]

2、對(duì)于數(shù)組(Array)而言

* 與 multiply均表示的是數(shù)量積(即對(duì)應(yīng)元素的乘積相加),np.matmul與np.dot表示的是矢量積(即矩陣乘法)。

代碼:

if __name__ == '__main__':
    w = np.array([[1,2],[3,4]])
    x = np.array([[1,3],[2,4]])
    w1 = np.array([[1,2],[3,4]])
    x1 = np.array([[1,2]])
    w_mat = np.mat([[1,2],[3,4]])
    x_mat = np.mat([[1,3],[2,4]])
    print("x1.shape:",np.shape(x1))
    w_x_start = w*x
    w_x_dot = np.dot(w,x)
    x_w_dot = np.dot(x,w)
    w_x_matmul = np.matmul(w, x)
    x_w_matmul = np.matmul(x, w)
    w_x_multiply = np.multiply(w,x)
    x_w_multiply = np.multiply(x, w)
    #w1_x1_matmul = np.matmul(w1, x1)
    x1_w1_matmul = np.matmul(x1, w1)
    w_x_mat_matmul = np.matmul(w_mat,x_mat)
    x_w_mat_matmul = np.matmul(x_mat, w_mat)
    w_x_mat_start = w_mat*x_mat
    x_w_mat_start = x_mat*w_mat
    w_x_mat_dot = np.dot(w_mat,x_mat)
    x_w_mat_dot = np.dot(x_mat,w_mat)
    w_x_mat_multiply = np.multiply(w_mat,x_mat)
    x_w_mat_multiply = np.multiply(x_mat,w_mat)
 
    print("W.shape:", np.shape(w))
    print("x.shape:", np.shape(x))
    print("w_x_start.shape:", np.shape(w_x_start))
    print("w_x_dot.shape:", np.shape(w_x_dot))
    print("x_w_dot.shape:", np.shape(x_w_dot))
    print("x1_w1_matmul.shape::", np.shape(x1_w1_matmul))
 
    print("做Array數(shù)組運(yùn)算時(shí):", '\n')
    print("w_x_start:", w_x_start)
    print("w_x_dot:", w_x_dot)
    print("x_w_dot:", x_w_dot)
    print("w_x_matmul:", w_x_matmul)
    print("x_w_matmul:", x_w_matmul)
    print("w_x_multiply:", w_x_multiply)
    print("x_w_multiply:", x_w_multiply)
    # print("w1_x1_matmul:", w1_x1_matmul)
    print("x1_w1_matmul:", x1_w1_matmul)
 
    print("做matrix矩陣運(yùn)算時(shí):", '\n')
    print("w_x_mat_start:", w_x_mat_start)
    print("x_w_mat_start:", x_w_mat_start)
    print("x_w_mat_dot:", x_w_mat_dot)
    print("w_x_mat_dot:", w_x_mat_dot)
    print("w_x_mat_matmul:",w_x_mat_matmul)
    print("x_w_mat_matmul:", x_w_mat_matmul)
    print("w_x_mat_multiply",w_x_mat_multiply)
    print("x_w_mat_multiply", x_w_mat_multiply)
x1.shape: (1, 2)
W.shape: (2, 2)
x.shape: (2, 2)
w_x_start.shape: (2, 2)
w_x_dot.shape: (2, 2)
x_w_dot.shape: (2, 2)
x1_w1_matmul.shape:: (1, 2)
做Array數(shù)組運(yùn)算時(shí):
 
w_x_start: [[ 1  6]
 [ 6 16]]
w_x_dot: [[ 5 11]
 [11 25]]
x_w_dot: [[10 14]
 [14 20]]
w_x_matmul: [[ 5 11]
 [11 25]]
x_w_matmul: [[10 14]
 [14 20]]
w_x_multiply: [[ 1  6]
 [ 6 16]]
x_w_multiply: [[ 1  6]
 [ 6 16]]
x1_w1_matmul: [[ 7 10]]
做matrix矩陣運(yùn)算時(shí):
 
w_x_mat_start: [[ 5 11]
 [11 25]]
x_w_mat_start: [[10 14]
 [14 20]]
x_w_mat_dot: [[10 14]
 [14 20]]
w_x_mat_dot: [[ 5 11]
 [11 25]]
w_x_mat_matmul: [[ 5 11]
 [11 25]]
x_w_mat_matmul: [[10 14]
 [14 20]]
w_x_mat_multiply [[ 1  6]
 [ 6 16]]
x_w_mat_multiply [[ 1  6]
 [ 6 16]]

python中轉(zhuǎn)置的優(yōu)先級(jí)高于乘法運(yùn)算 例如:

a = np.mat([[2, 3, 4]])
b = np.mat([[1,2,3]] )
d=a*b.T
print(d)

結(jié)果是

[[20]]

其中a為1行3列,b也為1行3列,按理來(lái)說(shuō)直接計(jì)算a*b是不能運(yùn)算,但是計(jì)算d=a*b.T是可以的,結(jié)果是20,說(shuō)明運(yùn)算順序是先轉(zhuǎn)置再計(jì)算a與b轉(zhuǎn)置的積,*作為矩陣乘法,值得注意的在執(zhí)行*運(yùn)算的時(shí)候必須符合行列原則。

numpy中tile()函數(shù)的用法

b = tile(a,(m,n)):即是把a(bǔ)數(shù)組里面的元素復(fù)制n次放進(jìn)一個(gè)數(shù)組c中,然后再把數(shù)組c復(fù)制m次放進(jìn)一個(gè)數(shù)組b中,通俗地講就是將a在行方向上復(fù)制m次,在列方向上復(fù)制n次。

python中的 sum 和 np.sum 是不一樣的,如果只寫(xiě)sum的話,表示的是數(shù)組中對(duì)應(yīng)的維度相加,如果寫(xiě) np.sum 的話,表示一個(gè)數(shù)組中的維數(shù)和列數(shù)上的數(shù)都加在一起。

如下圖所示:

補(bǔ)充:總結(jié):numpy中三個(gè)乘法運(yùn)算multiply,dot和* 的區(qū)別

引言:

本人在做機(jī)器學(xué)習(xí)的練習(xí)1的時(shí)候,時(shí)常拋出錯(cuò)誤:

Not aligned是什么意思呢?

意思是兩個(gè)矩陣相乘無(wú)意義。

線性代數(shù)中mxn 和 nxp的矩陣才能相乘,其結(jié)果是mxp的矩陣。

出錯(cuò)源代碼:

def gradientDescent(X,y,theta,alpha,iteration):
    colunms = int(theta.ravel().shape[1])
    thetai = np.matrix(np.zeros(theta.shape))
    cost = np.zeros(iteration)
                       
    for i in range(iteration):
        error = X*theta.T-y
        for j in range(colunms):
            a = np.sum(error*X[:,j])/len(X) ########## error!
            thetai[0,j] = thetai[0,j] - alpha*a
            
        theta = thetai    
        cost[i] = computeCost(X, y, theta)        
    return theta,cost

這里error是一個(gè)nx1的矩陣,theta.T也是一個(gè)nx1的矩陣。

而矩陣之間*運(yùn)算符表示矩陣乘法。我們這里想實(shí)現(xiàn)矩陣的對(duì)應(yīng)元素相乘,因此應(yīng)該用np.multiply()實(shí)現(xiàn)。

總結(jié):

(讀者可使用簡(jiǎn)單的舉例自行驗(yàn)證)

1.*用法:

矩陣與矩陣:矩陣乘法(matrix)

數(shù)組與數(shù)組:對(duì)應(yīng)位置相乘(array)

2.np.dot()用法:

矩陣相乘的結(jié)果

3.np.multiply()用法:

數(shù)組、矩陣都得到對(duì)應(yīng)位置相乘。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Python NumPy灰度圖像的壓縮原理講解
  • Python多進(jìn)程共享numpy 數(shù)組的方法
  • python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
  • python圖像處理基本操作總結(jié)(PIL庫(kù)、Matplotlib及Numpy)
  • 淺談Python numpy創(chuàng)建空數(shù)組的問(wèn)題
  • Python NumPy中diag函數(shù)的使用說(shuō)明
  • Python機(jī)器學(xué)習(xí)三大件之一numpy
  • python利用numpy存取文件案例教程

標(biāo)簽:駐馬店 常州 山東 六盤(pán)水 蘭州 江蘇 成都 宿遷

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python numpy中multiply與*及matul 的區(qū)別說(shuō)明》,本文關(guān)鍵詞  python,numpy,中,multiply,與,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python numpy中multiply與*及matul 的區(qū)別說(shuō)明》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于python numpy中multiply與*及matul 的區(qū)別說(shuō)明的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章