What is the numpy.multiply() function in Python?
Overview
The numpy.multiply() function in Python is used to multiply arguments element-wise.
Syntax
numpy.multiply(x1, x2, out=None)
Parameters
The multiply() function takes the following parameter values.
x1, x2: These represent the input arrays to be multiplied.out: This represents a location into which the result is stored. This is optional.
Return value
The multiply() function returns the element-wise product of the x1 and x2 arrays.
Code example
import numpy as np# creating the input arrrays to be multipliedx1 = np.arange(6).reshape(3, 2) + 1x2 = np.arange(2)# calling the multiply() functionmyarray = np.multiply(x1, x2)print(myarray)
Code explanation
- Line 1: We import the
numpymodule. - Line 3: We create an array,
x1, using thearange()function with three rows and two columns using thereshape()function. - Line 4: We create another array,
x2, with two elements, using thearange()function. - Line 7: We implement the
multiply()function on both arrays. The result is assigned to a new variablemyarray. - Line 9: We print the variable
myarray.
Note: It is worth noting that when implementing the
multiply()function on arrays, they must have the same shape.