Multiplication is the fundamental arithmetic operation that we utilize when we have two numbers or arrays and we need a product of both. In machine learning, we have large datasets to handle so we cannot do manual multiplication using calculators or basic operators. Here comes numpy.multiply() function.
In this article, we will understand Python numpy.multiply() function, its syntax, and we will also see the variety of techniques using which multiplication can be done on arrays with the help of this function. Let’s get started.
Also Read: numpy.arange() in Python
Syntax of numpy.multiply() Function
Let us get started by understanding the fundamental constituents of the numpy.multiply() function with the help of its syntax given below.
numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
- x1, x2: The input arrays for multiplication.
- out (optional): Output array in which the result is placed.
- where (optional): A boolean array indicating where the operation should occur.
- casting (optional): Controls what kind of data casting may occur.
- order (optional): ‘K’, ‘A’, ‘C’, or ‘F’ to control the memory layout of the output.
- dtype (optional): Desired data type for the output.
- subok (optional): If True, then the input arrays are allowed to be subclasses.
Examples of numpy.multiply() Function
Let us now look at some examples to demonstrate the use of numpy.multiply() function in Python.
Multiplication of Two Arrays
In this example, we will multiply two 1D arrays using numpy.multiply() function.
import numpy as np ar1 = np.array([1, 2, 3,4]) ar2 = np.array([8, 7, 4,9]) result= np.multiply(ar1, ar2) result
In the above code, we first imported the NumPy library as np which helped us create the arrays and let us use the numpy.multiply() for multiplication. After importing, we used it to create two 1D arrays ar1 and ar2. Then we multiplied both arrays to each other element-wise by passing it to the numpy.multiply() function and save the outcome in the result variable. Lastly, we printed the result variable.
We can see in the output that the first element of the first array and the first element of the second array got multiplied with each other and so on.
Multiplication of a Scalar Value with an Array
In this example, we will multiply an array to a scaler value using numpy.multiply().
import numpy as np ar = np.array([1,2,3,4]) sc= 13 result = np.multiply(ar, sc) result
Like above, here we have done the same thing but the slight difference is we multiplied a scaler value to an array. First, we took a scaler value and named it sc, then used the numpy.multiply() function we multiplied the scaler value with each element from the array, then stored the outcome in the result variable and printed it.
The output clearly shows that the scaler value is multiplied with every element from the array.
Using the ‘out’ Parameter for Multiplication
Like the previous example, here we will multiply two 1D arrays but we will also find out what will be the effect of the ‘out’ parameter while multiplying using numpy.multiply().
import numpy as np ar1 = np.array([11,22,33,44]) ar2 = np.array([4,3,2,1]) result= np.zeros_like(ar1) #zeros array creation with same shape as first array np.multiply(ar1,ar2,out=result) result
Here, firstly we created two 1D arrays: ar1 and ar2. Then we used numpy.zeros_like() function and passed ar1 as an argument which gave us the array of zeros with the same shape as ar1, we then saved the outcome into a result variable. Then we used numpy.multiply() and passed both the arrays ar1 and ar2 as an argument in the function with that we used ‘out’ parameter and passed the name of the array (array of zeros) i.e. the result variable, so that the output which we will get from the numpy.multiply() function can be stored in the result variable. After that, we printed the result variable.
This shows that the output array that numpy.multiply() function return after multiplying ar1 and ar2 has been stored in the result variable.
Using the ‘where’ Parameter for Multiplication
In this example, we will see the change in the output when we pass the ‘where’ parameter with the two arrays in the numpy.multiply() function.
import numpy as np ar1 = np.array([2, 3, 4]) ar2 = np.array([8, 2, 5]) conditionValues = np.array([False, False, True]) result = np.multiply(ar1, ar2, where=conditionValues) result
Here we have passed ar1 and ar2 with conditionValues, which means when the first item of the first array ar1 and the first item of the second array ar2 will be taken then because the first item in the conditionValues array is False, the multiplication can’t be performed and the items of the ar2 will be returned.
The last item which is the third item in the conditionValues array is True so the multiplication of the third item of the first array ar1 with the third item of the second array ar2 will only be performed, that’s why 4*5 which is 20 is present in the result array.
Using ‘casting’ Parameters for Data-Casting Control
In this example, we will create two 1D arrays of both different datatypes. The first array of datatype int32 and the second of datatype float64. With these two arrays, we will also pass ‘unsafe’ to the ‘casting’ parameter in the numpy.multiply() function which will be responsible for the control of data-casting occurring during the multiplication.
import numpy as np ar1 = np.array([3, 1, 2], dtype=np.int32) ar2 = np.array([1.1, 2.2, 3.3], dtype=np.float64) result = np.multiply(ar1, ar2,casting='unsafe') #Allow unsafe casting print(result)
In this tutorial, we have discussed numpy.multiply() function provided by Python’s NumPy library, its syntax, and parameters, and also explored a few examples to get an understanding of it. After reading this tutorial, we hope you easily can do the multiplication by using numpy.multiply() function in Python.