Element wise multiplication of a 2D and 1D array in python
<p>Lets say I have two numpy arrays:</p>

<pre><code>import numpy as np
x = np.array([[1,2,3], [4,5,6], [7,8,9]])
y = np.array([-1, 1, -1])
</code></pre>

<p>I want to multiply x and y in such a way that I get z:</p>

<pre><code>z = np.array([[-1,2,-3], [-4,5,-6], [-7,8,-9]])
</code></pre>

<p>In other words, if element j of y is -1, then all elements of the j-th row of x get multiplied by -1. If element k of y is 1, then all elements of the j-th row of x get multiplied by 1. </p>

<p>How do I do this?</p>

<p>Simply use the multiplication operator:</p>

<pre><code>x * y
Out[6]: 
array([[-1,  2, -3],
       [-4,  5, -6],
       [-7,  8, -9]])
</code></pre>

python numpy