How to Multiply NumPy array | np.Multiply

In this post, we are going to learn about how to Multiply NumPy array. We are going to learn this with the help of many examples. To run all the below programs the Numpy library must be installed on the system and if the numpy library is installed on our system we can import it into our program. By the end of this post, you will be able to answer the below questions.

  • NumPy Program to Multiply 2 Scaler numbers
  • NumPy Program to linear or 1-D Numpy Array
  • NumPy Program to two Multiply multi-dimensional Array
  • How to do element wise multiplication in numpy.

Numpy Multiply() function


In the python Numpy library, we have multiply() function that is used to do the element-wise multiplication of two arrays.

Syntax

numpy.multiply(arr1, arr2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'multiply'>

Parameters

ParametersMandatory or NotDetails
arr1Mandatory           First Input arrays to be multiplied.
arr2Mandatory           Second Input arrays to be multiplied
out: [ndarray, optional] Not Mandatory
A location into which the result is stored.
-> If provided, it must have a shape that the inputs broadcast to.
-> If not provided or None, a freshly-allocated array is returned.
-> A tuple (possible only as a keyword argument) must have a length equal to the number of outputs.
whereNot-MandatoryThis condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.
casting Not-Mandatory
order Not-Mandatory
dtype Not-Mandatory The type of the returned array. By default, the dtype of arr is used.
subok Not-Mandatory
unfunc Not-Mandatory

1. NumPy Program to Multiply 2 Scaler numbers


In this python program, we are using the np.multiply() function to multiply two scalar numbers by simply passing the scalar numbers as an argument to np.multiply() function.

import numpy as np
num1 = 5
num2 = 4
   
product = np.multiply(num1, num2) 
print ("Multiplication Result is : ", product) 

Output

Multiplication Result is : 20

2. NumPy Program to linear 1D Numpy Array


In this python program, we have used np.multiply() function to multiply two 1D numpy arrays by simply passing the arrays as arguments to np.multiply() function. This is how to multiply two linear arrays using np. multiply() function.

import numpy as np
arr1 = np.array([1, 2, 3, 4, 5] )
arr2 = np.array([5, 4, 3, 2, 1] )
   
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)

product = np.multiply(arr1, arr2) 
print ("Multiplication Result of 2D array is : ", product) 


Output

1st Input array :  [1 2 3 4 5]
2nd Input array :  [5 4 3 2 1]
Multiplication Result of 2D array is :  [5 8 9 8 5]

3. NumPy Program to Multiply multi-dimensional Numpy Array


In this python program example, we have used np. multiply() function to multiple 2D or multiply multi-dimensional Numpy Array

import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[6, 5, 4], [3, 2, 1]])
   
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)
   
product = np.multiply(arr1, arr2) 
print ("Multiplication Result of 2D array is : ", product) 


Output

1st Input array :  [[1 2 3]
 [4 5 6]]
2nd Input array :  [[6 5 4]
 [3
 2 1]]
Multiplication Result of 2D array is :  [[ 6 10 12]
 [12 10  6]]

4.Multiply Two Numpy Arrays With Different Shapes?


We can not multiply Two Numpy Arrays With Different Shapes by using multiply() function. If you will try to do this then it will give you an error like this:ValueError: operands could not be broadcast together with shapes (2,3) (2,).

5.How to do element wise multiplication in numpy


In this python program example, we are doing element-wise mutiplication of numpy arrays.

import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[6, 5, 4], [3, 2, 1]])
   
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)
   
product = np.multiply(arr1, arr2) 
print ("element wise mutiplication of numpy array : ", product) 

Output

 [4 5 6]]
2nd Input array :  [[6 5 4]
 [3 2 1]]
element wise mutiplication of numpy array :  [[ 6 10 12]
 [12 10  6]]