Numpy Element Wise Multiplication using numpy.multiply() method

Numpy Element Wise Multiplication featured image

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

Element wise array multiplication of 1D Array
Element wise array multiplication of 1D Array

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

Element wise array multiplication of 2 D Array
Element wise array multiplication of 2 D Array

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.

"Time

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.

"Output

 

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.

Element wise multiplication of Array of different size
Element wise multiplication of Array of different size

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

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner