Elementwise multiplication of several arrays in Python Numpy
Your fault is in not reading the documentation:
numpy.multiply(x1, x2[, out])
multiply
takes exactly two input arrays. The optional third argument is an output array which can be used to store the result. (If it isn't provided, a new array is created and returned.) When you passed three arrays, the third array was overwritten with the product of the first two.
For anyone stumbling upon this, the best way to apply an element-wise multiplication of n np.ndarray
of shape (d, )
is to first np.vstack
them and apply np.prod
on the first axis:
>>> import numpy as np
>>>
>>> arrays = [
... np.array([1, 2, 3]),
... np.array([5, 8, 2]),
... np.array([9, 2, 0]),
... ]
>>>
>>> print(np.prod(np.vstack(arrays), axis=0))
[45 32 0]