一、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)
结果图:
原文地址:https://www.cnblogs.com/wodexk/p/10311233.html
时间: 2024-10-05 07:56:47