numpy.matlib.identity()

This function is used to return an identity matrix of the given size. An identity matrix is the one with diagonal elements initializes to 1 and all other elements to zero.

Syntax

snippet
numpy.matlib.ones(size,dtype)

Parameters

It accepts the following parameters.

  1. shape: It is the number of rows and columns in the resulting identity matrix.
  2. dtype: It is the data type of the identity matrix.

Return

It returns an identity matrix of the specified size and specified data type.

Example

snippet
import numpy as np  
  
import numpy.matlib  
  
print(numpy.matlib.identity(4))

Output:

Output
[[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]]

Example: Identity matrix with integer values

snippet
import numpy as np  
  
import numpy.matlib  
  
print(numpy.matlib.identity(4,int))

Output:

Output
[[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +