numpy.identity() in Python
numpy.identity() in Python
The identity() method of Python numpy class returns an identity matrix i.e., a square matrix with ones on the main diagonal.
Syntax
numpy.identity(n, dtype=None)
Parameters
The numpy. identity() method consists of two parameters, which are as follows:
N : It represents the number of rows(or columns).
dtype : It is an optional parameter. It depicts the data type of returned array, and by default, it is a float.
Return Value
The numpy.identity () method returns identity array of dimension n x n, with its main diagonal set to one, and all other elements equal to zero.
Example 1
# Python Programming giving an example for # numpy.identity() method import numpy as numpy # 2x2 matrix with 1's on main diagnol obj1 = numpy.identity(3, dtype = float) print("Matrix : \n", obj1) obj2 = numpy.identity(4) print("\nMatrix : \n", obj2)
Output
Matrix : [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] Matrix : [[ 1. 0. 0. 0.] [ 0. 1. 0. 0.] [ 0. 0. 1. 0.] [ 0. 0. 0. 1.]]