# NumPy Identity Matrix | NumPy identity() Explained in Python

Hello geeks and welcome in this article, we will cover the NumPy identity matrix denoted as NumPy identity(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples. But at 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 function NumPy identity() helps us with this and returns an identity matrix as requested by you. 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.

Contents

## 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.

## PARAMETER

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

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

## EXAMPLES

As we are done with all the theory portion related to NumPy identity(). This section will be looking 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. Here at first, we have imported the NumPy module. Following which we have 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)):

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. Here, like the first example, we have first imported the NumPy library. After which, we have defined an array for which we want to find out the identity matrix. After which, we have defined our identity matrix. Then we have defined a result similar to our matrix size, which will be updated later. Then we have used the for loop to find 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 do 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 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. Now let us look at an example that will make things crystal clear to you.

## 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 getting an identity matrix as desired by the user.

I hope this article was able to clear all of your doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this why not read about auto clicker next.

Subscribe
Notify of 