1

I have two arrays with shapes:

z1.shape
(74L, 1L)

z2.shape
(74L,)

why does on multiplication it produces 74x74 size array:

z3 = np.multiply(z1,z2)
z3.shape
(74L, 74L)

I was expecting an element by element multiplication to achieve z3 of shape (74L, 1L)

How do I achieve the z3 as element by element multiplication of z1 and z2

2

3 Answers 3

1

You can do z1*z2[:,None]. The extra None index stretches the 1D array into a column array.

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

Comments

1

Just do,

z3 = np.multiply(z1, z2.reshape(-1,1))

Comments

1

To answer your question, you need to reshape either array:

>>> np.multiply(z1.reshape(-1), z2).shape
(74,)

>>> np.multiply(z1, z2[:,np.newaxis]).shape
(74, 1)

Unless the shapes of z1, z2 are identical, the multiplication (or any function for that matter) will be cast as an outer product (z1 * z1.T), resulting in the shape (74,74) in your case. In the two examples above,

  • z1.reshape(-1) casts the first array to the shape of the second (74,);
  • z2[:,np.newaxis] (or None instead of np.newaxis) casts the second to the shape of the first (74,1).

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.