You can use NumPy broadcasting for pairwise elementwise multiplication between x and y and then flatten with .ravel(), like so -
(x[:,None]*y).ravel()
Or use outer product and then flatten -
np.outer(x,y).ravel()
Answer from Divakar on Stack OverflowYou can use NumPy broadcasting for pairwise elementwise multiplication between x and y and then flatten with .ravel(), like so -
(x[:,None]*y).ravel()
Or use outer product and then flatten -
np.outer(x,y).ravel()
Use Numpy dot...
>>> import numpy as np
>>> a=np.arange(1,3)# [1,2]
>>> b=np.arange(1,4)# [1,2,3]
>>> np.dot(a[:,None],b[None])
array([[1, 2, 3],
[2, 4, 6]])
>>> np.dot(a[:,None],b[None]).ravel()
array([1, 2, 3, 2, 4, 6])
>>>
matrix - Elementwise multiplication of NumPy arrays of different shapes - Stack Overflow
How can arrays with different dimensions be multiplied? - Python - Data Science Dojo Discussions
python 2.7 - numpy multiply arrays with different shapes - Stack Overflow
python - how to multiply 2 numpy array with different dimensions - Stack Overflow
You can use np.multiply to multiply element-wise with broadcasting:
A = np.array([[1,1,1],
[1,1,1]])
B = np.array([2, 3])
res = np.multiply(A, B[:, None])
print(res)
array([[2, 2, 2],
[3, 3, 3]])
Simplest way is just (A*B.T).T But it's probably better to get used to broadcasting:
A * B[:, None]
This is functionally identical to @jpp's answer with np.multiply, but a little shorter to write
When doing an element-wise operation between two arrays, which are not of the same dimensionality, NumPy will perform broadcasting. In your case Numpy will broadcast b along the rows of a:
import numpy as np
a = np.array([[1],
[2]])
b = [3, 4]
print(a * b)
Gives:
[[3 4]
[6 8]]
To prevent this, you need to make a and b of the same dimensionality. You can add dimensions to an array by using np.newaxis or None in your indexing, like this:
print(a * b[:, np.newaxis])
Gives:
[[3]
[8]]
Let's say you have two arrays, a and b, with shape (2,3) and (2,) respectively:
a = np.random.randint(10, size=(2,3))
b = np.random.randint(10, size=(2,))
The two arrays, for example, contain:
a = np.array([[8, 0, 3],
[2, 6, 7]])
b = np.array([7, 5])
Now for handling a product element to element a*b you have to specify what numpy has to do when reaching for the absent axis=1 of array b. You can do so by adding None:
result = a*b[:,None]
With result being:
array([[56, 0, 21],
[10, 30, 35]])
Broadcasting involves 2 steps
give all arrays the same number of dimensions
expand the
1dimensions to match the other arrays
With your inputs
(41,6) (41,)
one is 2d, the other 1d; broadcasting can change the 1d to (1, 41), but it does not automatically expand in the other direction (41,1).
(41,6) (1,41)
Neither (41,41) or (6,41) matches the other.
So you need to change your y to (41,1) or the x to (6,41)
x.T*y
x*y[:,None]
I'm assuming, of course, that you want element by element multiplication, not the np.dot matrix product.
Not exactly sure, what you are trying to achieve. Maybe you could give an example of your input and your expected output. One possibility is:
import numpy as np
x = np.array([[1, 2], [1, 2], [1, 2]])
y = np.array([1, 2, 3])
res = x * np.transpose(np.array([y,]*2))
This will multiply each column of x with y, so the result of the above example is:
array([[1, 2],
[2, 4],
[3, 6]])
Hi everyone,
I'm trying to achieve something with the least possible overhead. I have a one-dimensional array A whose shape is (N,) and another one B whose shape is (M,N). What I'm trying to do is to element-wise multiply each column of B (axis 1) by A. Basically something like this:
C = np.zeros((M,N))
for m in range(M):
C[m,:] = A*B[m,:]This can also be achieved more succintly with:
C = np.tile(A, (M, 1))*B
The problem with the first approach is that it requires a for, and the problem with the second is that is unnecessarily creates a temporary array. I was wondering if there was some way to do this with some funky numpy stuff, perhaps with some tricky broadcasting but I haven't been able to come up with anything. Hopefully you will!!
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
If you don't want to used np.tile, you can use np.newaxis:
>>> A = np.arange(30).reshape((10, 3))
>>> B = np.arange(10)
>>> C = A*B[..., np.newaxis]
>>> C.shape
>>> (10, 3)