What is the numpy.reciprocal() function in NumPy?

Overview

The numpy.reciprocal() function in NumPy is used to compute the reciprocal of an argument x , which has been passed to it.

The reciprocal of a number is the value obtained when 1 is divided by that number.

Mathematically, this is represented as follows:

numpy.reciprocal(x) = 1x\frac{1}{x}

Syntax

numpy.reciprocal(x, /, out=None, *, where=True)
The syntax for the numpy.reciprocal() function

Parameter values

The numpy.reciprocal() function takes the following parameter values.

  • x: This represents input array and is a required parameter.
  • out: This represents the location where the result is stored and is an optional parameter. 
  • where: This is the condition over which the input is being broadcast. The resulting array will be set to the ufunc result at a given location where this condition is True. Otherwise, the resulting array will retain its original value. This is an optional parameter.
  • **kwargs: This represents other keyword arguments and is an optional parameter.

Example

import numpy as np
# creating an array
x = np.array([0.5, 1, 2])
# implementing the numpy.reciprocal() function
myarray = np.reciprocal(x)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array x using the array() method.
  • Line 7: We implement the np.reciprocal() function on the array. The result is assigned to a variable, myarray.
  • Line 9: We print the myarray variable.

Free Resources