5

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 ?

7 Answers 7

9

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Yes, Using transpose in turn around the columns to arrows . That helps. Thanks
3

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

Comments

2

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]

Comments

0

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

y*x

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.