Define the identity matrix in NumPy

The identity() in NumPy returns the n-dimension identity matrix.

import numpy as np

i1 = np.identity(1)
i2 = np.identity(2)
i3 = np.identity(3)
i4 = np.identity(4)

print(i1)
# [[1.]]

print(i2)
# [[1. 0.]
#  [0. 1.]]

print(i3)
# [[1. 0. 0.]
#  [0. 1. 0.]
#  [0. 0. 1.]]

print(i4)
# [[1. 0. 0. 0.]
#  [0. 1. 0. 0.]
#  [0. 0. 1. 0.]
#  [0. 0. 0. 1.]]

The main diagonals are ones and non diagonal elements are zeros. The determinants of the identity matrices are equal to one.

Comments

Powered by Markdown