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]
Answer from choucavalier on Stack OverflowElementwise multiplication of several arrays in Python Numpy - Stack Overflow
python - Multiplication of two arrays in numpy - Stack Overflow
How to multiply two arrays of matrices in Python?
Element-wise multiply arrays with different axes [numpy]
Are you using arrays? If yes, you're in luck because the '*' is element by element multiplication and not matrix multiplication. Therefore:
import numpy as np
A = np.array([5, 4, 3, 2, 1])
B = np.array([[ 123., 4., 0., 0., 0.],
[ 0., 0., 0., 45., 0.],
[ 0., 0., 100., -5., 0.]])
C = np.tile(A, (M, 1)) * B
D = A * B And as result you get:
>>> C
array([[ 615., 16., 0., 0., 0.],
[ 0., 0., 0., 90., 0.],
[ 0., 0., 300., -10., 0.]])
>>> D
array([[ 615., 16., 0., 0., 0.],
[ 0., 0., 0., 90., 0.],
[ 0., 0., 300., -10., 0.]])And if you're working with matrices, use multiply() to have element-wise multiplication (which is what you're trying to do)
https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html#array-or-matrix-which-should-i-use
More on reddit.comVideos
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]
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.
One way is to use the outer function of np.multiply (and transpose if you want the same order as in your question):
>>> np.multiply.outer(x, y).T
array([[3, 6],
[4, 8]])
Most ufuncs in NumPy have this useful outer feature (add, subtract, divide, etc.). As @Akavall suggests, np.outer is equivalent for the multiplication case here.
Alternatively, np.einsum can perform the multiplication and transpose in one go:
>>> np.einsum('i,j->ji', x, y)
array([[3, 6],
[4, 8]])
A third approach is to insert a new axis in one the arrays and then multiply, although this is a little more verbose:
>>> (x[:, np.newaxis] * y).T
array([[3, 6],
[4, 8]])
For those interested in performance, here are the timings of the operations, from quickest to slowest, on two arrays of length 15:
In [70]: x = np.arange(15)
In [71]: y = np.arange(0, 30, 2)
In [72]: %timeit np.einsum('i,j->ji', x, y)
100000 loops, best of 3: 2.88 µs per loop
In [73]: %timeit np.multiply.outer(x, y).T
100000 loops, best of 3: 5.48 µs per loop
In [74]: %timeit (x[:, np.newaxis] * y).T
100000 loops, best of 3: 6.68 µs per loop
In [75]: %timeit np.outer(x, y).T
100000 loops, best of 3: 12.2 µs per loop
You can you use np.outer.
In [7]: x = np.array([1, 2])
In [8]: y = np.array([3, 4])
In [10]: np.outer(x,y).T
Out[10]:
array([[3, 6],
[4, 8]])