1
$\begingroup$

Using NumPy and attempting to multiply matrices together sometimes doesn't work. For example

import numpy as np

x = np.matrix('1, 2; 3, 8; 2, 9')
y = np.matrix('5, 4; 8, 2')

print(np.multiply(x, y))

can return

Traceback (most recent call last):
  File "vector-practice.py", line 6, in <module>
    print(np.multiply(x, y))
ValueError: operands could not be broadcast together with shapes (3,2) (2,2)

I understand that I can't multiply these shapes, but why not? I can multiply these two matrices on paper, so why not in NumPy? Am I missing something obvious here?

$\endgroup$
1
  • 6
    $\begingroup$ Welcome to scicomp.SE. This site is intended for questions combining science and computation and for this type of pure package problem Stackoverflow may be more appropriate. However the clue to your problem is the definition of numpy.multipy "Multiply arguments element-wise". You may be intending matrix multiplication, which is provided by numpy.dot instead. $\endgroup$ – origimbo Sep 13 '17 at 22:54
1
$\begingroup$

Use np.dot() not np.multiply().

np.multiply() is for element-wise multiplication.

$\endgroup$
1
  • 1
    $\begingroup$ I think elementwise is a better choice of wording. $\endgroup$ – percusse Sep 14 '17 at 10:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.