numpy.identity() in Python
numpy.identity()
function is used to create an identity matrix which is used to make identity matrix. This is commonly used in linear algebra and numerical computations. It has the following properties:
- Diagonal elements are all 1s.
- Non-diagonal elements are all 0s.
Syntax: numpy.identity(n, dtype=None)
where:
- n : It takes int value and is Dimension n x n of output array
- dtype : It returns Data type of returned array. It is optional and by default it is float.
In the below example we use numpy.identity()
to create identity matrices of size 2x2 and 4x4 with 1
s on the diagonal and 0
s elsewhere. The dtype=float
specifies that the matrix elements should be float type.
import numpy as geek
b = geek.identity(2, dtype = float)
print("Matrix b : \n", b)
a = geek.identity(4)
print("\nMatrix a : \n", a)
Output :

It is u
seful for linear algebra operations like matrix multiplication, transformations and solving equations.