I wish to multiply two numpy matrices A and B_transformed:
A =
[[-1.910095 ]
[-1.20056174]
[-0.77669163]
[ 0.62406999]
[ 1.1471159 ]
[ 2.11616247]]
B =
[[ 0.70710678 -0.70710678]
[ 0.70710678 0.70710678]]
B_transformed = B[1,:]
= [0.70710678 0.70710678]
I tried:
product = np.dot(A,B_transformed)
However I get ValueError:
ValueError: shapes (6,1) and (2,) not aligned:
By matrices rule, (6,1) X (1,2) is allowed. Then why am I getting valueError?
product = [[-1.35064113 -1.35064113]
[-0.84892534 -0.84892534]
[-0.54920392 -0.54920392]
[ 0.44128412 0.44128412]
[ 0.81113343 0.81113343]
[ 1.49635283 1.49635283]]
Because the shape of B_transformed
is (2,)
, based on numpy
broadcasting, numpy
cannot find a way to broadcast B_transformed
in order to perform matmul
with A
.
In order to get the desired output, you need
np.matmul(A,B_transformed[None,:])
B_transformed[None,:]
will reshape B_transformed
to (1,2)