NumPy Identity Matrix | NumPy identity() Explained in Python

Hello geeks, and welcome! This article will cover the NumPy identity matrix denoted as NumPy identity(). Along with that, we will also look at its syntax and parameters for a better overall understanding. Then, we will see the application of all the theory parts through a couple of examples. But first, let us try to analyze the function through its definition.

An identity matrix is defined as a square matrix (equal number of columns and rows) with all the diagonal values equal to 1. At the same time, all the other places have a value of 0. The NumPy identity() function helps us with this and returns an identity matrix as you requested. The identity matrix is also known as the multiplicative identity for a square matrix. The identity matrix finds its importance when computing the inverse of a matrix and several other proofs.

Syntax

numpy.identity(n, dtype=None)

This is the general syntax for our function. In the next section we will see the various parameters associated with it.

Parameters

1. n:int

This parameter represents the number for which we desire to get the identity matrix.

2. dtype:data-type

This represents the data type of our identity matrix. By default, it is equal to “float”.

Return

OUT: NADARRAY

On completion of the program, it returns an identity matrix as requested by the user.

Examples

As we are done with all the theory portions related to NumPy identity(), This section will look at how this function works and how it helps us achieve our desired output. We will start with an elementary-level example and gradually move our way to more complicated examples.

1. Basic example for NumPy identity matrix

import numpy as ppool
a=ppool.identity(3)
print(a)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Above, we can see an elementary example of the NumPy identity matrix. First, we have imported the NumPy module. Following this, we used a print statement along with our array to get the desired output. Here, we can see the identity matrix has the data type of float, as we have not defined anything else. The main motive of this example was to make you aware of the usage of the syntax.

2. NumPy identity() as multiplication identity

import numpy as ppool

A=ppool.array([[16,2], [12,4]])
print(A)

B=ppool.identity(2,dtype=int)
print(B)

result=[[0,0], [0,0]]

for i in range(len(A)): 
    for j in range(len(B[0])): 
        for k in range(len(B)): 
            result[i][j] += A[i][k] * B[k][j] 

for r in result: 
    print(r) 

Output:

[[16  2]
 [12  4]]
[[1 0]
 [0 1]]
[16, 2]
[12, 4]

In the above example, we can see one of the many applications of the identity matrix. Like in the first example, we first imported the NumPy library. After this, we have defined an array for which we want to find out the identity matrix. After this, we defined our identity matrix.

Then, we have defined a result similar to our matrix size, which will be updated later. Then, we used the for loop to find and carry forward the matrix multiplication. In the end, our output justifies our input.

Why don’t you try out for the 3*3 matrix and tell me what result you got?

NumPy identity() vs NumPy eye()

This section will look at the difference between 2 of the NumPy functions, as mentioned above. We have already discussed the NumPy identity in this article. Let us look at the definition of NumPy eye. The function returns a 2-d matrix with all non-diagonal terms equal to 0.

Whereas the diagonal terms are equal to 1. Like the identity matrix, the difference here is that the diagonal can be shifted up or down. Then, the matrix cannot be called an identity matrix anymore. For a complete analysis of NumPy eye(), you can refer to this article. Let us look at an example that will make things crystal clear to you.

NumPy identity()NumPy eye()
import numpy as ppool
a=ppool.identity(3)
print(a)
import numpy as ppool
a=ppool.eye(3,k=1)
print(a)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]

Conclusion

In this article, we covered the NumPy identity matrix. Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that the function NumPy identity helps us get an identity matrix as desired by the user.

I hope this article was able to clear all of your doubts. But if you have any unsolved queries, feel free to write them down below in the comment section.

If you’re done reading this, why not read about auto clicker next?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments