一、Numpy的點積和dot矩陣相乘
dot()使用需注意:前一個矩陣的行數要與后一個矩陣的列數一致
import numpy as np print("============點積:A*B,對應位置相乘===============") A = np.array( [[1,1], [0,1]] ) B = np.array( [[2,0], [3,4]] ) print ("A=","\n",A) print ("B=","\n",B) print ("A*B=","\n",A*B) # A.*B =[[1*2,1*0] # [0*3,1*4]] # =[[2,0] # [0,4]] print("=============dot:矩陣相乘======================") print("-----書寫格式1:A.dot(B)-----") print (A.dot(B)) # A.dot(B)=[[1*2+1*3,1*0+1*4] # [0*2+1*3,0*0+1*4]] # =[[5,4] # [3,4]] print("-----書寫格式2:np.dot(A, B)-----") print (np.dot(A, B)) # A.dot(B)=[[1*2+1*3,1*0+1*4] # [0*2+1*3,0*0+1*4]] # =[[5,4] # [3,4]]
結果圖:
代碼2:dot()使用需注意:前一個矩陣的行數要與后一個矩陣的列數一致
import numpy as np a=np.ones((1,3,5)) print(a) b=np.ones((5,6))*3 print(b) c=a.dot(b) print(c.shape) print(c)
結果圖: