NumPy reciprocal() Function:
Numpy reciprocal: The reciprocal() function of the NumPy module calculates the reciprocal of all the elements in the input array.
In terms of array broadcasting, it is similar to 1/a.
Syntax:
numpy.reciprocal(a, out=None, dtype=None)
Parameters
a: This is required. It is an array given as input.
out: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.
dtype: This is optional. It indicates the data type of the array that will be returned.
Return Value:
The element-by-element reciprocal of the given array is returned.
NumPy reciprocal() Function in Python
Example1: (For 1-Dimensional Array)
Approach:
- Import numpy module using the import keyword.
- Pass some random list (1-Dimensional) as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the above-given array, datatype as a float as the arguments to the reciprocal() function of the numpy module to get the element-by-element reciprocal of the given array.
- Store it in another variable.
- Print the element-by-element reciprocal of the given array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list() as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([-10, 6, 4, 3, -20, 1]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the above given array, datatype as int as the arguments to the reciprocal() # function of the numpy module to get the element-by-element reciprocal of the given array # Store it in another variable. rslt = np.reciprocal(gvn_arry, dtype=float) # Print the element-by-element reciprocal of the given array. print("The element-by-element reciprocal of the given array:") print(rslt)
Output:
The above given array is: [-10 6 4 3 -20 1] The element-by-element reciprocal of the given array: [-0.1 0.16666667 0.25 0.33333333 -0.05 1. ]
Example2: (For n-Dimensional Array)
Approach:
- Import numpy module using the import keyword.
- Pass some random list (3-Dimensional) as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the above-given array, datatype as float as the arguments to the reciprocal() function of the numpy module to get the element-by-element reciprocal of the given array.
- Store it in another variable.
- Print the element-by-element reciprocal of the given array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list(3-Dimensional) as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([[10, 6, 4],[5, 10, 50],[20, 10, 8]]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the above given array, datatype as float as the arguments to the reciprocal() # function of the numpy module to get the element-by-element reciprocal of the given array # Store it in another variable. rslt = np.reciprocal(gvn_arry, dtype=float) # Print the element-by-element reciprocal of the given array. print("The element-by-element reciprocal of the given array:") print(rslt)
Output:
The above given array is: [[10 6 4] [ 5 10 50] [20 10 8]] The element-by-element reciprocal of the given array: [[0.1 0.16666667 0.25 ] [0.2 0.1 0.02 ] [0.05 0.1 0.125 ]]