NumPy Element Wise Multiplication

NumPy multiply() function is used to compute the element-wise multiplication of the two arrays with the same shape or multiply one array with a single numeric value. This function provides several parameters that allow the user to specify what value to multiply with.

Use numpy.multiply() function to multiplicate the first array elements (arr) with the second array elements (arr1). In this article, I will explain how to use the NumPy multiply() function and using it to returns an array that contains the multiplication of an input array.  

1. Quick Examples of Element Wise Multiplication

If you are in a hurry, below are some quick examples of how to use NumPy element-wise multiplication.


# Below are the quick examples

# Example 1: Get multiplication values using multiply()
arr = [2, 4, 6, 8, 5, 7] 
arr2 = np.multiply(arr,3)

# Example 2: use numpy.multiply() function to multiplication two numbers  
arr = 5
arr1 = 8  
arr2 = np.multiply(arr, arr1) 

# Example 3: use numpy.multiply() function
arr2 = np.multiply(3, 9)

# Example 4: Get the multiplication use numpy.multiply() 
arr = np.array([2, 4, 6, 8, 1])
arr1 =np.array( [3, 5, 7, 9, 2])
arr2 = np.multiply(arr, arr1)

# Example 5: Use numpy.mutiply() function and 
# get the matrix multiplication
arr = np.array([[2, 4, 6, 8],[1, 3, 5, 7]])
arr1 = np.array([[2, 3, 5, 4],[8, 5, 3, 2]])
arr2 = np.multiply(arr, arr1)

# Example 6: Get the certain rows multiplication
arr2 = np.multiply(arr[1,:],arr1[0,:])
arr3 = np.multiply(arr[ 0,: 2], arr1[ 1,: 1])
arr4 = np.multiply(arr[ 1,: 3], arr1[ 0,: 1])

# Example 7: Find the multiplication with the * operator
arr = np.array([[2, 4, 6, 8],[1, 3, 5, 7]])
arr1 = np.array([[2, 3, 5, 4],[8, 5, 3, 2]])
arr2 = arr * arr1

2. Syntax of NumPy multiply()

Following is the syntax of numpy.multiply() function.


# Syntax of numpy.multiply() 
numpy.multiply(arr, arr1, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = 

2.1 Parameters of NumPy multiply()

Following are the parameters of multiply().

  • arr – First input array or object works as a multiplication.
  • arr1 – Second input array or object which works as a multiplication.
  • out – It is ndarray, None, or tuple of ndarray and None, optional. Out will be the location where the result is to be stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where – This condition is broadcast over the input. The out array will be set to a function(ufunc) result in locations where the condition is True, whereas if the False value means to leave the value in output only.
  • **kwargs – Allows passing keyword variable length of argument to a function. Used when we want to handle a named argument in a function.
  • order – {‘C’, ‘F’, ‘A’, ‘K’}, optional: ‘C’: means to flatten in row-major using C-style order. ‘F’: means to flatten in column-major (Fortran- style) order. ‘A’: means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. K’: means to flatten an in the order the elements occur in memory. By default, the ‘C’ index order is used.
  • dtype – It is an optional parameter that specifies the data type of the returned array.

2.2 Return value of multiply()

It returns an array that contains multiplication values of an input arrays.

3. Usage of NumPy multiply() Function

The numpy.multiply() is a mathematical function and is used to calculate the multiplication between two NumPy arrays. Returns a multiplication of the inputs, element-wise.

We can multiply the array with a scalar value, to do so, we have taken an array named arr as a multiplicated and the scalar value 3 which indicates the multiplication. Now we will pass the array and scalar value as an argument in numpy.multiply() function. First, we have to create an NumPy array and then, apply this function.


import numpy as np

# creating an 1D input array
arr = [2, 4, 6, 8, 5, 7] 

# Get multiplication values using multiply()
arr2 = np.multiply(arr,3)
print(arr2)

# Output
# [ 6 12 18 24 15 21]

4. Use numpy.multiply() Function To Multiplication Two Numbers

If either arr or arr1 is 0-D(scalar) then numpy.multiply(arr,arr1) is equivalent to the multiplication of two numbers (a*b).


arr = 5
arr1 = 8

# use numpy.multiply() function to multiplication two numbers    
arr2 = np.multiply(arr, arr1) 
print (arr2) 

# Output
# 40

# use numpy.multiply() function
arr2 = np.multiply(3, 9)
print (arr2) 

# Output
# 27

5. Multiplication NumPy Array Elementwise

Using the multiply() function we can multiply two single dimension arrays elementwise, In the below example I am multiplying the arr by the arr1.


# creating two input array
arr = np.array([2, 4, 6, 8, 1])
arr1 =np.array( [3, 5, 7, 9, 2])

# Get the multiplication use numpy.multiply() 
arr2 = np.multiply(arr, arr1)
print(arr2)

# Output
# [ 6 20 42 72  2]

7. Use NumPy.multiply() with Two Dimension Arrays

Let’s perform element-wise multiplication using NumPy.multiply() function on 2-D arrays. This multiplies every element of the first matrix by the equivalent element in the second matrix using element-wise multiplication, or Hadamard Product. Make sure that the dimensions of both matrices have the same in order to multiply.


# Create a numpy two dimensional arrays
arr = np.array([[2, 4, 6, 8],[1, 3, 5, 7]])
arr1 = np.array([[2, 3, 5, 4],[8, 5, 3, 2]])
                 
# Use numpy.mutiply() function and 
# get the matrix multiplication
arr2 = np.multiply(arr, arr1)
print(arr2)

# Output
# [[ 4 12 30 32]
#  [ 8 15 15 14]]

To pass certain rows, columns, or submatrices to the numpy.multiply() function and get the multiplication of certain rows, columns, and submatrices. We should follow the same sizes of the rows, columns, or submatrices that we pass as our operands. Let’s take an example,


# Get the certain rows multiplication
arr2 = np.multiply(arr[1,:],arr1[0,:])
print(arr2)

# Output
# [ 2  9 25 28]

arr3 = np.multiply(arr[ 0,: 2], arr1[ 1,: 1])
print(arr3)

# Output
# [16 32]

arr4 = np.multiply(arr[ 1,: 3], arr1[ 0,: 1])
print(arr4)

# Output
# [ 2  6 10]

8. Multiplication NumPy Arrays With the * Operator

We can also use the * operator to multiply two arrays. The result is the same as the above.


# Find the multiplication with the * operator
arr2 = arr * arr1
print(arr2)

# Output
# [[ 4 12 30 32]
#  [ 8 15 15 14]]

9. Conclusion

In this article, I have explained how to use numpy.multiply() function and using how to calculate the multiplication of NumPy arrays on multi dimension (2-D) and 1-D arrays.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing NumPy Element Wise Multiplication