NumPy Tutorial NumPy Statistics NumPy References

NumPy - reciprocal() function



The NumPy reciprocal() function returns the reciprocal of the argument, element-wise. The syntax for using this function is given below:

Note: It is equivalent to 1/x in terms of array broadcasting.

Syntax

numpy.reciprocal(x, out=None, dtype=None)

Parameters

x Required. Specify the input array.
out Optional. Specify 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.
dtype Optional. Specify the desired data type of returned array.

Return Value

Returns reciprocal of the input array, element-wise.

Example:

The example below shows the usage of reciprocal() function.

import numpy as np
Arr = np.array([[2,4],[5,10]])

print("Arr is:")
print(Arr)

#reciprocal of Arr
print("\nReciprocal array is:")
print(np.reciprocal(Arr, dtype=float))

The output of the above code will be:

Arr is:
[[ 2  4]
 [ 5 10]]

Reciprocal array is:
[[0.5  0.25]
 [0.2  0.1 ]]

❮ NumPy - Functions