# numpy.reciprocal() in Python: Get Reciprocal of Array Elements

The numpy.reciprocal() function in Python is used to compute the reciprocal of each element in an array. The reciprocal of a number is simply 1 divided by that number, which means let’s say we have a number ‘a’ so the reciprocal of ‘a’ will be ‘1/a’. This function is part of the NumPy library, which is widely used for numerical and mathematical operations in Python.

In this article, we will understand Python numpy.reciprocal() function, its syntax, and demonstrate it with various examples. Let’s get started.

Also Read: numpy.min() in Python – Get Minimum Value in Array

## Syntax of numpy.reciprocal() Function

Let us get started by understanding the fundamental constituents of the numpy.reciprocal() function with the help of its syntax given below.

Syntax:

```numpy.reciprocal(array, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
```

Parameters:

• array: The input array for which we want to compute the reciprocals.
• out (optional): The output array in which the result is placed.
• where (optional): A boolean array indicating where the operation should occur.
• casting (optional): Controls what kind of data casting may occur.
• order (optional): ‘K’, ‘A’, ‘C’, or ‘F’ to control the memory layout of the output.
• dtype (optional): Desired data type for the output.
• subok (optional): If True, then the input arrays are allowed to be subclasses.

Return:

It returns the array with the same shape as the input array which will contain the reciprocal of each element of the input array.

## Examples of numpy.reciprocal() Function

Let us now look at some examples to demonstrate the use of numpy.reciprocal() function in Python.

### Reciprocal of a Scalar Value

First, let’s start by simply calculating the reciprocal of a scalar value using numpy.reciprocal().

Example:

```import numpy as np
sc=21.1
result = np.reciprocal(sc)
print(result)
```

In the above code, we first imported NumPy library as np then we took a scaler value and named it as sc. After that, we passed that scaler value into the numpy.reciprocal() function and we got the reciprocal of our number as an outcome. So it’s that simple to compute the reciprocal of any value using numpy.reciprocal() function.

Output:

### Reciprocal of a 1D Array

In this example, we will calculate the reciprocal of each element of a one-dimensional array using numpy.reciprocal().

Example:

```import numpy as np
ar= np.array([1, 15, 33, 0.5,47])
result = np.reciprocal(ar)
print(result)
```

By using NumPy we created a 1D array and stored it into a variable called ar. Then to get the reciprocal of each element from the array we passed our array into numpy.reciprocal() function and stored the outcome into a result variable. In last, we printed our result variable.

Output:

We can clearly see that we got the reciprocal of each element very easily by just passing the array into the function.

### Reciprocal of a 2D Array

This time the only difference will be that we will have a two-dimensional array and we will compute the reciprocal of each and every element of a 2D array like we did for the 1D array in the last example.

Example:

```import numpy as np
ar = np.array([[14,3],[2,3.2],[8,1]])
result = np.reciprocal(ar)
print(result)
```

Here we created a 2D array using a NumPy library and then passed it to the numpy.reciprocal() function to get the reciprocal of every element from the array.

Output:

Like the last example here we also got the reciprocal of every element from our 2D array.

### Reciprocal of an Array Containing Zeros

As we know in mathematics if we reciprocate 0 then we get infinite. So let’s see that when our array contains zeros and we compute the reciprocal of this array using numpy.reciprocal() then what will happen.

Example:

```import numpy as np
ar = np.array([11,60,2,0,4.5,0,2])
result = np.reciprocal(ar)
print(result)
```

In the above code, we created an array containing zeros and then we passed that array into the numpy.reciprocal() function. After we ran the above code, we got infinite as a reciprocal.

Output:

## Summary

In this tutorial, we have discussed numpy.reciprocal() function provided by Python’s NumPy library, its syntax, and parameters, and also explored a few examples to get an understanding of it. After reading this tutorial, we hope you easily can do the reciprocal of each element of a NumPy array.

## Reference

https://numpy.org/doc/stable/reference/generated/numpy.reciprocal.html

Articles: 44