Python

# Numpy Element Wise Multiplication

NumPy is a Python package for array processing. It features a high multidimensional array object as well as manipulation tools. It is the most significant Python package for scientific computing. Just a few of the features include a strong N-dimensional array object, complex functions, handy linear algebra, Fourier transform, plus random number capabilities, to name a few. In addition to its obvious scientific applications, NumPy could be used as multi-dimensional storage of generalized data. NumPy allows arbitrary data types to be created, allowing NumPy to connect with a wide range of databases cleanly and quickly.

Now we will get to the meet of our conversation: NumPy element wise multiplication. This article will show you how to execute element-wise matrix multiplication in Python using several methods. In this multiplication, every element of the initial matrix is multiplied by the relevant part of the second matrix. Both matrices should have the same dimensions when doing element-wise matrix multiplication. The size of the resultant matrix ‘c’ of element-wise matrix multiplication a*b = c is always the same as that of a and b. We can conduct element-wise multiplication in Python using the various methods presented in this article. However, when we wish to compute the multiplication of two arrays, we utilize the numpy.multiply() function. It returns the element-wise combination of arr1 and arr2.

## Example 1:

In this example, the np.multiply() technique will be used to do the element-wise multiplication of matrices in Python. The NumPy library’s np.multiply(x1, x2) method receives two matrices as input and executes element-wise multiplication over them before returning the resultant matrix. We must send the two matrices as input to the np.multiply() method to execute element-wise input. The example code below explains how to execute the element-wise multiplication of two matrices using Python’s np.multiply() method. You can see that we constructed two one-dimensional numpy arrays (A and B) with identical shape and then multiplied them element by element. [10, 16, 43, 5, 7] ,[2, 4, 7, 2, 5] items make up array A, while [15, 43, 5, 71, 44],[31, 7, 8, 2, 3] elements make up array B. The element wise multiplication of values in A and B produces values in the final array, as can be seen.

import numpy as np

A = np.array([[10,16,43,5,7],[2,4,7,2,5]])

B = np.array([[15,43,5,71,44],[31,7,8,2,3]])

print(np.multiply(A,B)) Here is the result. ## Example 2:

The np.multiply() method can also be used to perform element-wise multiplication of specified rows, columns, and even submatrices. The precise rows, columns, or even submatrices must be sent to the np.multiply() method. In element-wise matrix multiplication, the dimensions of the rows, columns, or submatrices given as the first and second operands are the same. The code demonstrates the element-wise multiplication of columns, rows, or submatrices of two matrices in Python. Below we have [21, 34, 12, 5, 1] , [2, 4, 7 , 2 ,5] elements in array A, and [11, 13, 1, 123, 32],[21 ,7 ,8 ,2 ,3] elements in array B. The result is obtained by executing element-wise multiplication of selected rows, columns, or submatrices of the matrices.

import numpy as np

A = np.array([[21,34,12,5,1],[2,4,7,2,5]])

B = np.array([[11,13,1,123,32],[21,7,8,2,3]])

print(np.multiply(A[0,:],B[1,:]))

print(np.multiply(A[1,:],B[0,:]))

print(np.multiply(A[:,3],B[:,1])) Below is the result obtained after element-wise multiplication. ## Example 3:

The * operator will now be used to do element-wise matrices multiplication in Python. When used with matrices in Python, the * operator returns the resultant matrix of element-wise matrix multiplication. The example code below shows how to execute element-wise matrix multiplication in Python using the * operator. We’ve designated two distinct arrays with the values [23, 13, 33, 2, 6], [4, 6, 9, 2, 7]) and [22, 61, 4, 11, 43], [2, 7, 2, 5, 3]) in this example.

numpy as np

A = np.array([[23,13,33,2,6],[4,6,9,2,7]])

B = np.array([[22,61,4,11,43],[2,7,2,5,3]])

print(A*B) The result was presented after performing the * operation between the two arrays. ## Example 4:

The * operator in Python can also be used to do element-wise multiplication of rows, columns, and even submatrices of matrices. in our last example, two arrays with the values [22, 11, 12, 2, 1],[5, 7, 9, 6, 2] and [11, 5, 4, 6, 12],[7 ,7, 1, 9, 5] have been created. Then, on defined rows, columns, and submatrices, we conduct element-by-element multiplication.

import numpy as np

A = np.array([[22,11,12,2,1],[5,7,9,6,2]])

B = np.array([[11,5,4,6,12],[7,7,1,9,5]])

print(A[0,:]*B[1,:])

print(A[1,:]*B[0,:])

print(A[:,3]*B[:,1]) Attached is the output. ## Conclusion:

In this post, we have discussed numpy, which is Python’s essential package for scientific computing. It’s a Python library that includes a multidimensional array object, derivative objects (such as masked arrays and matrices), and a variety of functions for performing quick array operations, such as mathematical, logical, shape manipulation, sorting, and so on. Apart from numpy, we have talked about element-wise multiplication, commonly known as the Hadamard Product, which involves multiplying each element in a matrix by its equivalent element on a secondary matrix. Use the np.multiply() function or the * (asterisk) character in NumPy to execute element-wise matrix multiplication. These procedures can only be carried out on matrices of the same size. We’ve gone over these strategies in depth so that you may easily implement the rules in your own programs. 