numpy.identity () in Python



numpy.identity (n, dtype = None): return the identity matrix, that is, the square matrix with ones on the main daignol. 
Parameters :

  n:  [int] Dimension nxn of output array  dtype:  [optional, float (by Default)] Data type of returned array. 

Returns :

 identity array of dimension nxn, with its main diagonal set to one, and all other elements 0. 

# Python Programming Illustrative
# numpy.identity method

 

import numpy as geek

 
Matrix # 2x2 with 1 on the main diagnosis

b = geek.identity ( 2 , dtype = float )

print ( "Matrix b: " , b)

  

 

a = geek.identity ( 4 )

print ( " Matrix a: " , a)

Output:

 Matrix b: [[1. 0.] [0. 1.]] Matrix a: [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] 

Notes:
These codes will not work for online IDs. Please run them on your systems to see how they work.

This article is courtesy of Mohit Gupta_OMG