NumPy Tutorial NumPy Statistics NumPy References

NumPy - matlib.identity() function



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

Syntax

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

Parameters

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.

Example:

The example below 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