Open In App

numpy matrix operations | identity() function

Last Updated : 21 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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 : Python3
# 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 : Python3
# 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]]

Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy