NumPy identity() Function:
Numpy identity function: The identity array is returned by the NumPy identity() function. The identity array is a square array with the major diagonal filled with ones.
Syntax:
numpy.identity(n, dtype=None)
Parameters
n: This is required. It represents the number of rows (and columns) in n x n output.
dtype: This is optional. It is the data type of the output. Float is the default value.
Return Value:
An n x n array with one as the main diagonal and all other entries set to 0 is returned.
NumPy identity() Function in Python
Example
Approach:
- Import numpy module using the import keyword.
- Pass some random number(n) as an argument to the identity() function to create an n x n array with the main diagonal set to 1 and all other entries set to 0 and store it in a variable.
- Print the above obtained first array.
- Pass some random number(n), datatype as int as an argument to the identity() function to create an n x n array with the main diagonal set to 1 and all other entries set to 0 (integer format) and store it in another variable.
- Print the above obtained second array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword
import numpy as np
# Pass some random number(n) as an argument to the identity() function to
# create an n x n array with the main diagonal set to 1 and all other entries set to 0
# and store it in a variable.
gvn_arry1 = np.identity(3)
# Print the above obtained first array.
print("The above obtained first array = \n", gvn_arry1)
# Pass some random number(n), datatype as int as an argument to the identity() function to
# create an n x n array with the main diagonal set to 1 and all other entries set to 0
# (integer format)and store it in another variable.
gvn_arry2 = np.identity(4, dtype=int)
# Print the above obtained second array.
print("The above obtained second array = \n", gvn_arry2)
Output:
The above obtained first array = [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] The above obtained second array = [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]
Example2
Here we give the datatype as the float.
# Import numpy module using the import keyword
import numpy as np
# Pass some random number(n) as an argument to the identity() function to
# create an n x n array with the main diagonal set to 1 and all other entries set to 0
# and store it in a variable.
gvn_arry1 = np.identity(3)
# Print the above obtained first array.
print("The above obtained first array = \n", gvn_arry1)
# Pass some random number(n), datatype as float as an argument to the identity() function to
# create an n x n array with the main diagonal set to 1 and all other entries set to 0
# (floating format)and store it in another variable.
gvn_arry2 = np.identity(4, dtype=float)
# Print the above obtained second array.
print("The above obtained second array = \n", gvn_arry2)
Output:
The above obtained first array = [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] The above obtained second array = [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]]