44

I have those arrays:

a = np.array([
     [1,2],
     [3,4],
     [5,6],
     [7,8]])

b = np.array([1,2,3,4])

and I want them to multiply like so:

[[1*1, 2*1],
[3*2, 4*2],
[5*3, 6*3],
[7*4, 8*4]]

... basically out[i] = a[i] * b[i], where a[i].shape is (2,) and b[i] then is a scalar.

What's the trick? np.multiply seems not to work:

>>> np.multiply(a, b)
ValueError: operands could not be broadcast together with shapes (4,2) (4)
2
  • 3
    The answer below uses a feature called broadcasting. You can read about it here, here and here. It's also more standard to use the operator * rather than multiply Commented Apr 8, 2014 at 11:20
  • 1
    @YXD. You're right, although there are 2 things at play here - first reshape, then broadcast together. Nevertheless, I would rather insert a link to this question in the documentation, than the other way round - the theory behind broadcasting sounds very complicated, and seeing a simple example like this one, or e.g. a multiplication table on 2 aranges (outer product) gives a good concrete example. Commented Nov 6, 2019 at 19:31

6 Answers 6

54

add an axis to b:

>>> np.multiply(a, b[:, np.newaxis])
array([[ 1,  2],
       [ 6,  8],
       [15, 18],
       [28, 32]])
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, thanks! I think I should spend some time figuring out what's the think with "axis" ...
a * b[:, np.newaxis] does not work for sparse matrices though. Using multiply works.
21

For those who don't want to use np.newaxis or reshape, this is as simple as:

a * b[:, None]

This is because np.newaxis is actually an alias for None.

Read more here.

Comments

12
>>> a * b.reshape(-1, 1)
array([[ 1,  2],
       [ 6,  8],
       [15, 18],
       [28, 32]])

1 Comment

So what? Answering on SX not only helps authors but also the people who will reach this page in the future when they've encountered the same problem.
7

What's missing here is the einsum (doc) variant:

np.einsum("ij,i->ij", a, b)

This gives you full control over the indices and a and b are passed blank.

1 Comment

As np.einsum was already available when the question was asked, I think this should be the accepted answer. The answer above uses additional memory I guess?
0

This is how a*b works when a is transposed so you can also do

np.transpose(np.transpose(a)*b)

Comments

-2

It looks nice, but quite naive, I think, because if you change the dimensions of a or b, the solution

np.mulitply(a, b[:, None])

doesn't work anymore.

I've always had the same doubt about multiplying arrays of arbitrary size row rise, or even, more generally, n-th dimension wise.

I used to do something like

 z = np.array([np.multiply(a, b) for a, b in zip(x,y)])

and that works for x or y that have dimension 1 or 2.

Does it exist with a method with "axis" argument like in other numpy methods? Such like

 z = np.mulitply(x, y, axis=0)

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.