Python Numpy eye() - Create Identity Matrix

Updated on November 18, 2024
eye() header image

Introduction

The eye() function in Python's NumPy library is essential for creating identity matrices, which are crucial in linear algebra and other mathematical computations. An identity matrix is a square matrix with ones on the diagonal and zeros elsewhere, serving as the neutral element in matrix multiplication.

In this article, you will learn how to utilize the eye() function to generate identity matrices of various sizes. Discover the simplicity of configuring matrix dimensions and understand how this function can be applied to different matrix-related operations.

Basic Usage of numpy.eye()

Create a Simple Identity Matrix

  1. Import the NumPy library.

  2. Use the eye() function to create an identity matrix of a specified size.

    python
    import numpy as np
    
    identity_matrix = np.eye(3)
    print(identity_matrix)
    

    This code creates a 3x3 identity matrix. Each row in the output contains exactly one element with a value of 1 (aligned diagonally), and all other elements are 0.

Specifying Data Type

  1. Identify the desired data type for the matrix elements.

  2. Pass the dtype argument to the eye() function to specify the data type.

    python
    import numpy as np
    
    identity_matrix_float = np.eye(3, dtype=float)
    print(identity_matrix_float)
    

    By setting dtype to float, each element in the matrix is of type float. This is particularly useful when the identity matrix is used in computations needing floating point precision.

Customizing the Identity Matrix

Adjusting the Width of the Matrix

  1. Use the N and M parameters to define the dimensions of the matrix.

  2. Generate a matrix that is not strictly square.

    python
    import numpy as np
    
    rectangular_identity = np.eye(3, 4)
    print(rectangular_identity)
    

    This generates a 3x4 matrix where the identity diagonal is still present, but the matrix is not square. This type of matrix is useful when dealing with transformation matrices in graphics and other applications.

Shifting the Diagonal

  1. Understand the use of the k parameter to shift the diagonal.

  2. Apply k to shift the diagonal either upwards or downwards.

    python
    import numpy as np
    
    shifted_identity = np.eye(4, k=1)
    print(shifted_identity)
    
    shifted_identity_neg = np.eye(4, k=-1)
    print(shifted_identity_neg)
    

    The first matrix shifts the diagonal up by one place, resulting in the '1's starting from the second column. In the second matrix, the diagonal shifts down, placing the '1's in the subdiagonal. This is useful in specific matrix operations, such as creating toeplitz matrices.

Conclusion

The eye() function in NumPy provides a straightforward way to create identity matrices, which are foundational in numerous mathematical computations and algorithms. By mastering the use of this function, as well as its parameters like N, M, k, and dtype, you effectively handle various matrix-related tasks in Python. Utilize these techniques to enhance your projects involving linear algebra, data transformation, or systems modeling, ensuring efficiency and effectiveness in your mathematical computations.