Numpy Multiply | How to Use Numpy.multiply() Function in Python

The Numpy multiply function is a part of numpy arithmetic operations. There are basic arithmetic operators available in the numpy module, which are add, subtract, multiply, and divide. The significance of python multiply is equivalent to the multiplication operation in mathematics.

What does Numpy Multiply Function do?

The numpy multiply function calculates the product between the two numpy arrays. It calculates the product between the two arrays, say x1 and x2, element-wise. The numpy.multiply() is a universal function, i.e., supports several parameters that allow you to optimize its work depending on the specifics of the algorithm.

Syntax of Numpy Multiply

numpy.multiply(a1, a2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘multiply’)

Parameters of Numpy Multiply

ParameterMandatory or Not
a1Mandatory
a2Mandatory
/Not-Mandatory
outNot-Mandatory
*Not-Mandatory
whereNot-Mandatory
castingNot-Mandatory
orderNot-Mandatory
dtypeNot-Mandatory
subokNot-Mandatory
unfuncNot-Mandatory
  • a1: [arrayLike]
    1st Input array for calculating the product.
  • a2[arrayLike]
    2nd input array for calculating the product.
  • out: [ndarray, None, or tuple of ndarray and None, optional]
    out will be the location where the result is to be stored. have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where: [array_like, optional]
    If the value of where is true it indicate to calculate the unfunc at that position, whereas if the value is false than it denotes to leave the value in output only.

Return Value of Numpy Multiply

The Numpy multiply function returns the product between a1 and a2. The multiply() function can be scalar of nd-array. It depends on the a1 and a2. Suppose a1, and a2 are scalar, then numpy. Multiply () will return a scalar value. Else it will return an nd-array.

Note: The input a1 and a2 must be broadcastable to a common shape (which becomes the shape of the output).

Examples of Numpy Multiply Function

Let’s go through the examples of Numpy multiply() function and see how it works.

Example 1: Using Np.multiply() Function To find multiplication of two numbers

import numpy as np
a1 = 24
a2 = 13

print ("1st Input number : ", a1) 
print ("2nd Input number : ", a2) 
	
mul = np.multiply(a1, a2) 
print ("Product of two input number : ", mul) 

Output:

1st Input number :  24
2nd Input number :  13
Product of two input number :  312

Explanation

In this simple first example, we just multiplied two numbers and get the result. Let’s take a look at each step and know what happens in each stage. First of all, we imported the numpy module as np it’s obvious because we are working on the numpy library. After that, we have taken two pre-defined inputs ’24’, ’13’, and stored them in variables ‘a1’, ‘a2’ respectively. We printed our inputs to check whether they are specified properly or not. Then the main part comes where we will find the product between the two numbers.

Herewith the help of the np.multiply() function, we will calculate the product between a1 and a2. This multiplication operation is identical to what we do in mathematics.

So, we will get the product between the number 24 and 13 which is 11.

Example 2: Using Np.multiply() Function to find the product between two input arrays

import numpy as np
a1 = [20, 21, 5, 9]
a2 = [13, 17, 6, 11]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
mul = np.multiply(a1, a2) 
print ("Product of two input arrays : ", mul) 

Output:

1st Input array :  [20, 21, 5, 9]
2nd Input array :  [13, 17, 6, 11]
Product of two input arrays :  [260 357  30  99]

Explanation

From this example, things get Lil bit tricky; instead of numbers, we have used arrays as our input value.
We can now see we have two input arrays a1 & a2 with array inputs [20, 21, 5, 9] and [13, 17, 6, 11], respectively. The numpy.multiply() function will find the product between a1 & a2 array arguments, element-wise.

So, the solution will be an array with the shape equal to input arrays a1 and a2. The product between a1 and a2 will be calculated parallelly, and the result will be stored in the mul variable.

Example 3: Using Np.multiply() Function To Find product Between Two Multi-Dimensional Arrays

import numpy as np
a1 = [[20, 21, 5], [-9, 11, 1]]
a2 = [[13, 17, 6], [1, -8, 7]]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
mul = np.multiply(a1, a2) 
print ("Product of two input arrays : ", mul) 

Output:

1st Input array :  [[20, 21, 5], [-9, 11, 1]]
2nd Input array :  [[13, 17, 6], [1, -8, 7]]
Product of two input arrays :  [[260 357  30]
[ -9 -88   7]]

Explanation

The third example in this numpy multiply() tutorial is slightly similar to the second example which we have already gone through. What we have done here in this example is instead of a simple numpy array we have used a multi-dimensional array in both of our input values a1 and a2.

Make sure both the input arrays should be of the same dimension and same shapes. The numpy.multiply() function will find the product between array arguments, element-wise.

Can We Find Product Between Two Numpy Arrays With Different Shapes?

In simple words, No, we can’t find products or use the numpy multiply function in two numpy arrays that have different shapes.

Let’s look it through one example,

import numpy as np
a1 = [[20, 21, 5], [-9, 11, 1]]
a2 = [[7, 13, 17, 6], [1, -8, 7]]

print ("1st Input array : ", a1) 
print ("2nd Input array : ", a2) 
	
mul = np.multiply(a1, a2) 
print ("Product of two input arrays : ", mul) 

Output:

ValueError: operands could not be broadcast together with shapes (2,3) (2,)

Explanation

If the shape of two numpy arrays is different, then we will get a value error. The value error will say something like, for example.

ValueError: operands could not be broadcast together with shapes (2,3) (2,)

Here in this example, we get a value error because the a2 input array has a different shape than the a1 input array. To get the product without any value error, make sure to check the shape of arrays.

What’s Next?

NumPy is mighty and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.

You might like our following tutorials on numpy.

Conclusion

The numpy multiply() is a compelling and essential function available in the numpy module. The numpy multiply() function can be handy and highly recommended. Many experts use this while finding the product between substantial data sets.

If you still have any questions regarding the NumPy multiply function. Leave your question in the comments below.

Happy Pythonning!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments