Multiply Function of Python Numpy Explained – 8 Examples

multiply function of python numpy explained with coding examples parameters explained in jupyter notebook

In this article, we’ll be understanding the multiply function of Python Numpy with the help of various easy code examples. To make things more clear, we’ll explain each coding example step by step. By the end of this article, you’ll have a detailed practical knowledge of how to use the Numpy multiply() function.

Use of Multiply function of Python Numpy Library

This function is used to execute the element-wise multiplication operation on the arrays that are passed to it as arguments. This functions returns a new array in which all the elements/items are a result of the multiplication between the two array’s corresponding items.

Images of Code Examples

multiply function of python numpy explained with coding examples parameters explained in jupyter notebook

python numpy.multiply() function explained with coding examples parameters explained

python numpy multiply function explained with coding examples parameters explained

Syntax of numpy.multiply()

numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Explaining the Parameters

  1. The parameters (x1 and x2) can be single/multi-dimensional arrays or scalar values.
  2. The optional parameter (out) specifies an array in which the resulting output will be stored. If it’s not specified, then the creation of a new array will take place to store the result.
  3. The optional parameter (where) specifies whether to perform the operation of multiplication or not. It takes a Boolean value. Passing it True will execute the operation, and passing it False will result in no changes made to the corresponding output array’s element.
  4. The parameter (casting) is also optional. The role of this parameter is to specify the kind of data casting while the operation is performed.
  5. The parameter(order) that is optional specifies the output’s array memory layout. The order can be C(row-major C style) or F(column-major Fortran style).
  6. The optional parameter(dtype) gives a specific datatype to the elements of the output array. If not specified, then the datatype will be specified using the items datatype of the existing arrays that will be passed to the Numpy multiply() function.
  7. The parameter(subok) is optional and it takes a Boolean value. If we pass it True then if possible, the newly generated array will be of the same subclass as the arrays that are provided to the multiply() function.

Implementing Python numpy.multiply() Function (8 Examples)

Example 1: numpy.multiply() applied on 1D arrays

import numpy as np

a1 = np.array([1, 2, 3,4])
a2 = np.array([4, 5, 6,7])

res1 = np.multiply(a1, a2)
print(res1)

Output

[ 4 10 18 28]

In this code:

  • We imported the Numpy library and made two numpy arrays of a single dimension(1D).
  • After that, we make a call to the multiply function and passed these 2 arrays as arguments to it. Also, we stored the result inside a variable and prints it.
  • The output shows that all the adjacent elements of the 2 arrays have gone through multiplication and the result is added to the new array created by the multiply() function.

Example 2: multiply() Function on an array and a scalar value

arr1D = np.array([5,4,7,8])
sc= 6
result = np.multiply(arr1D, sc)
print(result)

Output

[30 24 42 48]

Explaining the code

In this code, we have a 1D array and a scalar value. We passed both of them to the multiply function and the result shows that the scalar value has performed the multiplication operation with all the items of the array and the resulting array has stored the result of each multiplication which can be viewed in the above output block.

Example 3: parameter ‘out’ of multiply function

array1D = np.array([10,20,30,40])
array2D = np.array([5,2,3,4])
res= np.zeros_like(array1D) # zeros array creation with same shape as first array
np.multiply(array1D,array2D,out=res)
print(res)

Output

[ 50  40  90 160]

Code Explanation

In this example code:

  1. We created two arrays of single dimensions.
  2. Then we created an array of zeros using the zeros_like() function of Numpy and gave it a shape similar to the first array. You can use the second array as well. We just want to match the shape of this array with the resulting array.
  3. After that, we called the multiply function and passed the 2 arrays to it in the form of its arguments.
  4. Also, we used the out parameter of the multiply function and passed it the name of the array(simple array of zeros) that we created so the result can be stored inside it.
  5. By printing the result of this array, we can see that the resulting values have been stored inside this array. So in this way, we can set where we want to store the resulting values.

Example 4: Parameter ‘where’ of Numpy multiply()

arrr1 = np.array([2, 4, 9])
arrr2 = np.array([3, 5, 4])
conditionValues = np.array([False, False, True])
resultArray = np.multiply(arrr1, arrr2, where=conditionValues)
print(resultArray)

Output

[ 3  5 36]

Code Explained

We’ve 2 one dimensional arrays. Also, we have an array of boolean values. This array will be passed to the ‘where‘ parameter. The output shows that when the condition was false, no multiplication operation was performed.

For instance, when the first item of the first array and the first item of the second array was taken and the condition was checked then we can see that the first item of the array that we passed to the ‘where‘ parameter is False so it didn’t perform the operation of multiplication on it. The same process will be implemented for all the other items as well. The last condition is true so the operation of multiplication is performed on it and we can see it in the output.

We can also see that the value of the second parameter is shown when the condition is False.

Example 5: The parameter ‘casting’

a1 = np.array([1, 2, 3], dtype=np.int32)
a2 = np.array([3.6, 4.1, 5.8], dtype=np.float64)
result = np.multiply(a1, a2,casting='unsafe') # Allow unsafe casting
print(result)

Output

[ 3.6  8.2 17.4]

We’ve applied multiplication on the 2 arrays and also we passed the ‘unsafe’ to the casting parameter. The kind of data casting occurring during the operation is controlled by this parameter.

Example 6: Multiply function ‘order’ parameter with 3D array

ar2D1 = np.array([[[1, 2], [3, 4]],[[5, 6], [7, 8]]])
ar2D2 = np.array([[[9, 10], [11, 12]],[[13, 14], [15, 16]]])

resultC = np.multiply(ar2D1, ar2D2, order='C') 
resultF = np.multiply(ar2D1, ar2D2, order='F')

print(resultC)
print(resultF)

Output

[[[  9  20]
  [ 33  48]]
                   # c style
 [[ 65  84]
  [105 128]]]
[[[  9  20]
  [ 33  48]]
                 # Fortran style
 [[ 65  84]
  [105 128]]]

In this code, we used the 3D arrays, you can try it with 2D as well. We also specified the order parameter and passed it C(row-major C style) and F(column-major Fortran style). The result of using both these styles can be seen in the above output.

Example 7: The parameter ‘dtype’ of Python Numpy Multiply Function

ar1 = np.array([1, 2, 3])
ar2 = np.array([4, 5, 6])
fl1 = np.multiply(ar1, ar2, dtype=float)
in1 = np.multiply(ar1, ar2, dtype=int)
print(fl1) 
print(in1)

Output

[ 4. 10. 18.]
[ 4 10 18]

In this code, we’ve used the ‘dtype‘ parameter of the multiply function and passed it integer(int) and float(float). It actually specified the data type of items of the resulting array. If we don’t specify it using this parameter, then it’ll automatically take the data type by using the input array items datatype.

Example 8: ‘subok’ parameter of Numpy Multiply

class MySubArr(np.ndarray):
   pass

a1 = np.array([1, 2, 3,4,5])
a2 = np.array([4, 5, 6,7,8])
res = np.multiply(a1, a2, subok=True).view(MySubArr)
print(type(res))

Output

<class '__main__.MySubArr'>

In the example, we created a class. Also after passing 2 arrays to the multiply function, we specified the ‘subok‘ parameter to True. What it means is that if possible, then the result of element-wise multiplication should be of the same subclass as the input arrays that we’ve inputted.

Conclusion

In conclusion, we practically discussed the working of the multiply function of Python Numpy. We’ve gone through all the parameters of the multiply() function with coding examples and have explained them step by step. Hope you’ll now easily implement this function in your code.

Thank you so very much for reading this article.

Leave a Comment

Your email address will not be published. Required fields are marked *