Adam
Adam

Reputation: 169

how to multiply 2 numpy array with different dimensions

I try to multiply 2 matrix x,y with shape (41) and (41,6) as it is supposed to broadcast the single matrix to every arrow in the multi-dimensions

I want to do it as :

x*y

but i get this error

ValueError: operands could not be broadcast together with shapes (41,6) (41,) 

Is there anything I miss here to make that possible ?

Upvotes: 5

Views: 46527

Answers (7)

Henk
Henk

Reputation: 5

So, if x has shape (41,6) and y (41,), I'd use np.expand_dims() to add an empty, second dimension (index 1) to y, i.e.,

x * np.expand_dims(y, 1)

This will automatically yield a result with shape (41,6).

Upvotes: 0

user2660966
user2660966

Reputation: 975

The multiplication of a ND array (say A) with a 1D one (B) is performed on the last axis by default, which means that the multiplication A * B is only valid if

A.shape[-1] == len(B)

A manipulation on A and B is needed to multiply A with B on another axis than -1:

Method 1: swapaxes

Swap the axes of A so that the axis to multiply with B appear on last postion

C = (A.swapaxes(axis, -1) * B).swapaxes(axis, -1)

example

A = np.arange(2 * 3 * 4).reshape((2, 3, 4))
B = np.array([0., 1., 2.])
print(A)
print(B)

pormpts :

(A)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
(B) 
[0. 1. 2.]

A * B returns :

ValueError: operands could not be broadcast together with shapes (2,3,4) (3,)

now multiply A with B on axis 1

 axis = 1
 C = (A.swapaxes(axis, -1) * B).swapaxes(axis, -1)

returns C :

    array([[[ 0.,  0.,  0.,  0.],
            [ 4.,  5.,  6.,  7.],
            [16., 18., 20., 22.]],
    
           [[ 0.,  0.,  0.,  0.],
            [16., 17., 18., 19.],
            [40., 42., 44., 46.]]])

Note that first raws of A have been multiplied by 0 last raws have been multiplied by 2

Method 2: reshape B

make B have the same number of dimensions than A, place the items of B on the dimension to be multiplied with A

A * B.reshape((1, len(B), 1))

or equivalently using the convenient 'numpy.newaxis' syntax :

A * B[np.newaxis, :, np.newaxis]

Upvotes: 2

chatrapathi
chatrapathi

Reputation: 117

You can try out this, it will works!

>>> import numpy as np
>>> x = np.array([[1, 2], [1, 2], [1, 2]])
>>> y = np.array([1, 2, 3])
>>> np.dot(y,x)
array([ 6, 12])

Upvotes: 3

hpaulj
hpaulj

Reputation: 231335

Broadcasting involves 2 steps

  • give all arrays the same number of dimensions

  • expand the 1 dimensions 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.

Upvotes: 8

Thomas
Thomas

Reputation: 1377

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]])

Upvotes: 3

dcwhcdhw
dcwhcdhw

Reputation: 59

If you wish to multiply X of dimension (n) to Y of dimension(n,m), you may consider the answers from this post

Tips can be found in the Wikipedia as well:

In Python with the numpy numerical library or the sympy symbolic library, multiplication of array objects as a1*a2 produces the Hadamard product, but with otherwise matrix objects m1*m2 will produce a matrix product.

Simply speaking, slice it up to arrays and perform x*y, or use other routes to fit the requirement.

Upvotes: 0

B. Eckles
B. Eckles

Reputation: 1644

Depends on what you're expecting. One simple solution would be:

y*x

That should give you a matrix of dimensions (1,6).

Upvotes: 0

Related Questions