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 :
import numpy as geek
import numpy.matlib
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 :
import numpy as geek
import numpy.matlib
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]]
Last Updated :
21 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...