| by Arround The Web | No comments

Numpy Element Wise Multiplication

Python doesn’t have built-in arrays that can be used for representing the matrices. However, to represent the matrices in Python, the NumPy package is used to create arrays. Suppose the user wants to perform multiplication or element-wise multiplication on the arrays. To do this, the NumPy library provides a method “multiply(),” and this same function can be used to perform element-wise multiplication as well.

This post will illustrate the use of the multiply() method to perform array multiplication and element-wise multiplication.

How to Use “multiply()” Method of NumPy?

As explained above, this method is used to perform multiplication on arrays created through the NumPy package. Simply go over its syntax given below:

numpy.multiply(array1, array2, optionsParam)

 
In this syntax:

    • array1” denotes the first array in the multiplication.
    • array2” denotes the second array in the multiplication.
    • optionsParams” include different options that can be used to fine-tune the multiplication process.

Note: To learn about the optional parameters in the multiply() method, check out the official documentation from NumPy!

Example 1: Element Wise Multiplication With a Scalar Value

To perform scalar multiplication on every element of an array one by one, start by importing the numpy library into your program:

import numpy

 
After that, create a new array using the array() method from the numpy library:

array1 = numpy.array([[1,2,3], [4,5,6]])

 
Use the multiply() and pass a scalar value in the first argument and the array in the second argument:

result =numpy.multiply(4,array1)

 
Lastly, print out the result onto the terminal by using the print() method:

print(result)

 
The complete code snippet for this example is as:

import numpy
array1 = numpy.array([[1,2,3],[4,5,6]])
result =numpy.multiply(4,array1)
print(result)

 
When this code is executed, it produces the following result:


It is clear from the output image above that every element was multiplied with a scalar value.

Example 2: Element Wise Multiplication Between two Arrays

Start by importing the numpy library and creating two different arrays using the array() method:

import numpy
array1 = numpy.array([[1,2,3],[4,5,6]])
array2 = numpy.array([[4,5,6],[1,9,3]])

 
After that, pass both of the arrays in the arguments of the multiply() method and print out the result using the following lines:

result = numpy.multiply(array1,array2)
print(result)

 
Once this code is executed, it produces the following result:


The output verifies that both matrices/arrays have been multiplied using element-wise multiplication.

Alternative: Use the “*” Operator

Alternatively, the user can simply use the asterisk symbol instead of using the multiply() method as it is considered a short-hand operator for the multiply() method. To demonstrate this, simply take the following code:

import numpy
array1 = numpy.array([[1,2,3],[4,5,6]])
array2 = numpy.array([[4,5,6],[1,9,3]])
result = array1 * array2
print(result)

 
Running the above code will produce the following result:


The asterisk operator produced the same results as the multiply() method.

Conclusion

To perform element-wise multiplication on matrices created with the NumPy library, the user can utilize the multiply() method. This multiply() method takes two mandatory arguments, which are the arrays to be multiplied or a scalar and an array. Alternatively, the user can use the asterisk operator “*” to get the same results.

Share Button

Source: linuxhint.com

Leave a Reply