Facebook Page Twitter Page LinkedIn Page
× NumPy Tutorial NumPy Resources

The NumPy matlib.identity() function is used to return a square identity matrix of given size. The syntax for using this function is given below:


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


n Required. Specify the size of the returned identity matrix.
dtype Optional. Specify the desired data-type for the matrix. Default: float

Return Value

Returns a square identity matrix of given size.


The below example shows the usage of matlib.identity() function.

import numpy as np
import numpy.matlib

mat1 = np.matlib.identity(3)
print("mat1 is:\n", mat1)

#specifying int data type
mat2 = np.matlib.identity(3, dtype=int)
print("\nmat2 is:\n", mat2)

The output of the above code will be:

mat1 is:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

mat2 is:
[[1 0 0]
 [0 1 0]
 [0 0 1]]

❮ NumPy - Functions