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])
>>>
python - Efficient multiplying matrices with different shapes in numpy - Stack Overflow
matrix - Elementwise multiplication of NumPy arrays of different shapes - Stack Overflow
python 2.7 - numpy multiply arrays with different shapes - Stack Overflow
How does the matrix multiplication of different shapes is working?
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]])
In middle school, I learnt that matrices can only be added if they have the same shape (m x n) and (p x q) addition or subtraction is only possible if m == p and n == q. The operation is element-wise and that makes sense
How come different shape in NumPy array is added with each other
>>> a = np.random.randint(0, 5, (2, 3))
>>> b = np.random.randint(0, 5, (3, 1))
>>> c = np.random.randint(0, 5, (1, ))
>>> a
array([[4, 4, 4],
[4, 2, 4]])
>>> b
array([[0],
[3],
[0]])
>>> c
array([2])
>>> matmul = np.matmul(a, b)
>>> matmul
array([[12],
[ 6]])
>>> matmul.shape
(2, 1)
>>> c.shape
(1,)
>>> matmul + c
array([[14],
[ 8]])
>>> In the above code, I couldn't understand the last 3 steps
UPDATE: I want to know about matrix multiplication addition, my bad.
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]])