The identity()
function from numpy
library is used to return the identity matrix.
Note: An identity matrix is the type of matrix in which all the elements on the main diagonal are ones (1) and all other elements are zeros (0).
identity(n, dtype=float, like=None)
The identity()
function takes the following parameter values:
n
: This represents the number of rows in the output.dtype
: This represents the data type of the output matrix. This is optional and its default type is float.like
: This represents the matrix-like object or the prototype.The identity()
function returns a square identity of by with its main or principal diagonal set to 1
and all other elements to 0
.
from numpy import identity# creating an identity matrix with four rowsidentity_matrix = identity(4)print(identity_matrix)
identity()
from the numpy
module.identity()
function, we create an identity matrix with 4
rows. The output is stored in the variable identity_matrix
.identity_matrix
.