国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

python numpy中multiply與*及matul 的區別說明

瀏覽:4日期:2022-06-18 11:19:45
1、對于矩陣(matrix)而言

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

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

結果是

[[ 1 4 9 16 25]]a = np.mat([[1, 2, 3, 4, 5]])b = np.mat([ [1],[2],[3],[4],[5] ] )d=a*bprint(d) #a是shape(1,5),b是shape(5,1),結果是一個實數

結果是

[[55]]

2、對于數組(Array)而言

* 與 multiply均表示的是數量積(即對應元素的乘積相加),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數組運算時:', ’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矩陣運算時:', ’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數組運算時: 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矩陣運算時: 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中轉置的優先級高于乘法運算 例如:

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

結果是

[[20]]

其中a為1行3列,b也為1行3列,按理來說直接計算a*b是不能運算,但是計算d=a*b.T是可以的,結果是20,說明運算順序是先轉置再計算a與b轉置的積,*作為矩陣乘法,值得注意的在執行*運算的時候必須符合行列原則。

numpy中tile()函數的用法

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

python中的 sum 和 np.sum 是不一樣的,如果只寫sum的話,表示的是數組中對應的維度相加,如果寫 np.sum 的話,表示一個數組中的維數和列數上的數都加在一起。

如下圖所示:

python numpy中multiply與*及matul 的區別說明

補充:總結:numpy中三個乘法運算multiply,dot和* 的區別

引言:

本人在做機器學習的練習1的時候,時常拋出錯誤:

python numpy中multiply與*及matul 的區別說明

Not aligned是什么意思呢?

意思是兩個矩陣相乘無意義。

線性代數中mxn 和 nxp的矩陣才能相乘,其結果是mxp的矩陣。

出錯源代碼:

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-yfor 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是一個nx1的矩陣,theta.T也是一個nx1的矩陣。

而矩陣之間*運算符表示矩陣乘法。我們這里想實現矩陣的對應元素相乘,因此應該用np.multiply()實現。

總結:

(讀者可使用簡單的舉例自行驗證)

1.*用法:

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

數組與數組:對應位置相乘(array)

2.np.dot()用法:

矩陣相乘的結果

3.np.multiply()用法:

數組、矩陣都得到對應位置相乘。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 久久精品女人毛片国产 | 日韩精品中文字幕在线观看 | 欧美一级毛片俄罗斯 | 日韩一区二区三区在线播放 | 国产孕妇做受视频在线观看 | 私人毛片免费高清影视院丶 | 一级片免费的 | 久久久在线视频精品免费观看 | 国产免费怡红院视频 | 国产aⅴ片 | 久久久精品影院 | 综合欧美视频一区二区三区 | 欧美日韩一区二区在线观看视频 | 久草视频免费在线 | 欧美特一级 | www.九九| 免费高清一级欧美片在线观看 | 91欧美激情一区二区三区成人 | 成人免费看片 | 手机看黄av免费网址 | 在线观看自拍视频 | 日韩欧美在线视频一区二区 | 中文字幕在线视频精品 | 九九精品在线视频 | 欧美怡红院在线观看 | 日本韩国一级毛片中文字幕 | 国产精品videosse | 欧美一级黄 | 国产一级真人毛爱做毛片 | 在线a视频网站 | 免费 成年人 | 国产一级黄毛片 | 免费一级特黄3大片视频 | 久久网免费| 99在线观看巨臀大臀视频 | 国产三级精品美女三级 | 国产一级毛片一区二区三区 | 国产看色免费 | 九九午夜| 一级成人毛片 | 中文字幕 亚洲 一区二区三区 |