What is an identity matrix?
An identity matrix is a square matrix or an nxn matrix that contains ones in its main diagonal and all other elements are zero. For example, let’s say I3 is an identity matrix with 3 rows and 3 columns. So, I3 will contain the following elements:
How to create an identity matrix using Python NumPy?
We can use the following Python code to create an nxn identity matrix using the NumPy library.
import numpy n = 3 A = numpy.eye(n) print("A: \n", A)
Here, we are using the numpy.eye() function to create an identity matrix. The argument in the function indicates the number of rows or columns of the square matrix.
The output of the mentioned function will be:
A: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]






0 Comments