0

I have a 2D matrix for example X=np.ones((97,2)) and another vector for example Theta=np.zeros((2,1)) when I multiply the vector Theta and the Matrix X, it raises an error

'operands could not be broadcast together with shapes (2,1) (97,2)' 

but when I multiply Theta.T* x, it output a new (97, 2) matrix.

How did it work? How (1,2) *(97,2) works and if the shapes don't matter why it raises an error when Theta * X could anyone explain it, please.

1
  • 1
    Matrix multiplication should be X @ Theta. Theta.T * X is element-wise multiplication. Commented Jul 22, 2020 at 13:50

1 Answer 1

1

It tries to broadcast ( = replicate) the smaller array across the larger one when a matrix multiplication doesn't work by dimensions, which is why your second example works but the first one doesn't.

https://numpy.org/doc/stable/user/basics.broadcasting.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.