 # Numpy Element Wise Multiplication using numpy.multiply() method

Numpy is a python module for performing calculation on arrays. In this tutorial, I will show you how to do NumPy element wise multiplication with various examples.

Please note that I am coding all the examples on the Jupyter Notebook. You can do it on your IDEs but I will suggest you go with me for deep understanding.

## Element wise array multiplication in NumPy

In this section, I will discuss two methods for doing element wise array multiplication for both 1D and 2D. The first method is using the numpy.multiply() and the second method is using asterisk (*) sign.

### Multiplication of 1D array

``````array_1d_a = np.array([10,20,30])
array_1d_b = np.array([40,50,60])``````

Using numpy.multiply() method.

``np.multiply(array_1d_a,array_1d_b) ``

Using Asterisk Method

``array_1d_a * array_1d_b``

Output

### 2D array element wise multiplication

Lets create a 2D Numpy array.

``````array_2d_a = np.array([[10,20],[30,40]])
array_2d_b = np.array([[50,60],[70,80]])``````

Using numpy.multiply() method.

The 2D multiplication is the same as 1 D element wise multiplication.

``np.multiply(array_2d_a,array_2d_b)``

Using Asterisk Method

``array_2d_a * array_2d_b``

Output

There is a question among readers that which method should you choose.  As you already know the faster method is mostly preferred. So if you will use the multiply() method then you will get faster results. Looks at the time taken while doing multiplication using both methods. ## Element wise matrix multiplication in NumPy

The above example was element wise multiplication of NumPy array. In this section, you will learn how to do Element wise matrix multiplication. But before that let’s create a two matrix. In NumPy, you can create a matrix using the numpy.matrix() method. Just execute the code below.

``````mat1 = np.matrix([[1,2,3],[4,5,6]])
mat2= np.matrix([[7,8,9],[10,11,12]])``````

Matrix Multiplication

``mul_result = np.array(mat1)*np.array(mat2)``

The above result will be of type array. To change it to the matrix you have to pass the result as an argument inside the matrix() method.

``np.matrix(mul_result)``

The output of the above code is below. ## Element wise multiplication of Array of different size

If you have a NumPy array of different dimensions then you can do multiplication element wise. To achieve it you have to use the numpy.transpose() method. Execute the following code.

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

Here I am creating two NumPy array of 2×2 and 2×4 dimensions. If you directly multiply using the asterisk(* ) operator then you will get the dimension error. That’s why I am using the transpose() method.

``result = array_2x2* np.transpose((np.array([array_2x4,]*2)))``

You will get the following output.

That’s all for now. These are the examples for doing element wise multiplication of array using NumPy. I hope you understood it properly. Even if you don’t have understood it then you can contact us for more solutions.

Source:

Numpy Multiply Method Documentation 