Saturday, October 11, 2025
HomeLanguagesnumpy matrix operations | identity() function

numpy matrix operations | identity() function

numpy.matlib.identity() is another function for doing matrix operations in numpy. It returns a square identity matrix of given input size.

Syntax : numpy.matlib.identity(n, dtype=None)

Parameters :
n : [int] Number of rows and columns in the output matrix.
dtype : [optional] Desired output data-type.

Return : n x n matrix with its main diagonal set to one, and all other elements zero.

Code #1 :




# Python program explaining
# numpy.matlib.identity() function
  
# importing matrix library from numpy
import numpy as geek
import numpy.matlib
  
# desired 3 x 3 output square identity matrix 
out_mat = geek.matlib.identity(3) 
print ("Output matrix : ", out_mat) 


Output :

Output matrix :  
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

 

Code #2 :




# Python program explaining
# numpy.matlib.identity() function
  
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
  
# desired 5 x 5 output square identity matrix 
out_mat = geek.matlib.identity(n = 5, dtype = int) 
print ("Output matrix : ", out_mat) 


Output :

Output matrix :  
[[1 0 0 0 0]
 [0 1 0 0 0]
 [0 0 1 0 0]
 [0 0 0 1 0]
 [0 0 0 0 1]]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11882 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS