Numpy Identity Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn about the numpy.matlib.identity() function of the NumPy library. An identity matrix is a matrix in which all the diagonal elements are set to 1 and the other elements to 0. This function helps in generating an identity matrix of the given size and the data type specified.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-86448{{"`Numpy Identity Function`"}} python/numeric_types -.-> lab-86448{{"`Numpy Identity Function`"}} python/tuples -.-> lab-86448{{"`Numpy Identity Function`"}} python/importing_modules -.-> lab-86448{{"`Numpy Identity Function`"}} python/numerical_computing -.-> lab-86448{{"`Numpy Identity Function`"}} python/build_in_functions -.-> lab-86448{{"`Numpy Identity Function`"}} end

Import necessary libraries

To use the required functions, we must import the NumPy library. We would also like to import the matlib function.

import numpy as np
import numpy.matlib

Generate an Identity matrix

We will use the np.matlib.identity(n,dtype) function to generate an identity matrix of size n and data type dtype.

identity_matrix = np.matlib.identity(4)
print("Identity Matrix:\n", identity_matrix)

Output:

Identity Matrix:
 [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

Generate an Identity matrix with specified data type

We can specify the data type of the Identity matrix by using the dtype parameter.

identity_matrix = np.matlib.identity(4, dtype=int)
print("Identity Matrix of int type:\n", identity_matrix)

Output:

Identity Matrix of int type:
 [[1 0 0 0]
 [0 1 0 0]
 [0 0 1 0]
 [0 0 0 1]]

The difference between identity() and eye() functions

The eye() function is another NumPy function that generates matrices. The eye() function generates a matrix in which all the diagonal elements are set to 1 and the other elements are set to 0.

Let's generate a 3 by 3 matrix with the eye() function.

eye_matrix = np.eye(3)
print("Eye Matrix:\n", eye_matrix)

Output:

Eye Matrix:
 [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

As we can see, in the eye() function, we have set the size of the matrix using the parameters n, m or shape. We set the k parameter to determine the position of the diagonal. When k=0, the diagonal is in the main position, when k=1, the diagonal is one position above the main diagonal, and so on.

The main difference between these two functions is that the identity() function returns a square matrix having ones on the main diagonal like this:

[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

while the eye() function returns a matrix having 1 on the diagonal and 0 elsewhere, which is based on the value of k parameter. If the value of k > 0 then the diagonal is above the main diagonal, and vice versa.

Summary

In this lab, you learned about the NumPy matlib.identity() function and how it generates an identity matrix of the given size and data type. We also learned about the differences between identity() and eye() functions.

Conclusion

The numpy.matlib.identity() function can be used to generate an identity matrix of the given size and data type. The identity matrix is a matrix that has all diagonal elements set to 1, and all other elements set to 0. You can also use the eye() function to generate a matrix with diagonal elements set to 1 and other elements set to 0, with more options to determine the position of the diagonal elements.

Summary

Congratulations! You have completed the Numpy Identity() Function lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like