I have a problem in understanding the working behind the numpy dot function and broadcasting.Below is the snippet I am trying to understand
a=np.array([[1,2],[3,5]])
if we check the shape of a
a.shape
it will be (2,2)
b=np.array([3,6])
and b.shape is (2,)
Question1: is b
column vector or row vector? while providing input it seems b
is row vector but then shape shows it as a column vector having 2 rows.What is the fault in my understanding?
now if do
a.dot(b)
it result in
array([15,39])
Question2: as per matrix multiplication if a
is m*n
then b
must be n*k
and since a
is 2*2 then b
must be 2*1. Does this verify that b
is a column vector otherwise if it would a row vector then matrix multiplication shall not be possible, but the output of the dot product does give the value according to the matrix multiplication considering b
as column vector and broadcasting it
now b.dot(a)
is also possible and results in
array([21,36])
and
this blew my mind.How are they checking the compatibility of the vector for matrix multiplication and how do they calculate?
In at least one of the scenario, they must throw the error for incompatible dimension for multiplication.But it is not shown and they are computing the result in both of the cases.