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 multiplied
x1 = np.arange(6).reshape(3, 2) + 1
x2 = np.arange(2)
# calling the multiply() function
myarray = np.multiply(x1, x2)
print(myarray)

Code explanation

  • Line 1: We import the numpy module.
  • Line 3: We create an array, x1, using the arange() function with three rows and two columns using the reshape() function.
  • Line 4: We create another array, x2, with two elements, using the arange() function.
  • Line 7: We implement the multiply() function on both arrays. The result is assigned to a new variable myarray.
  • 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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources