NumPy Eye Function

numpy
Published

June 5, 2023

Understanding the NumPy eye Function

The numpy.eye function generates an array with ones on the diagonal and zeros elsewhere. This array is commonly known as an identity matrix, crucial in linear algebra and various mathematical computations. Its primary purpose is to create these specialized matrices quickly and efficiently, avoiding manual construction which can be prone to errors, especially for larger matrices.

Key Parameters of numpy.eye

The numpy.eye function accepts several key parameters:

  • N (integer): This mandatory parameter specifies the number of rows and columns in the square identity matrix. If you only need a square matrix, this is the only parameter you’ll need.

  • M (integer, optional): Allows you to create a rectangular matrix. N will define the number of rows, while M defines the number of columns. If M is omitted, a square matrix (N x N) is generated.

  • k (integer, optional): This parameter determines the position of the diagonal. A value of k=0 (default) positions the ones on the main diagonal. A positive k shifts the diagonal upwards, while a negative k shifts it downwards.

  • dtype (data-type, optional): Specifies the desired data type of the array elements (e.g., int, float, complex). The default is float64.

  • order ({'C', 'F'}, optional): Specifies the memory layout of the array. 'C' (default) indicates row-major order, while 'F' indicates column-major order.

Code Examples: Unveiling the Power of numpy.eye

Let’s illustrate the numpy.eye function with a series of practical examples:

Example 1: Creating a 3x3 Identity Matrix:

import numpy as np

identity_matrix = np.eye(3)
print(identity_matrix)

Output:

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

Example 2: Creating a 4x5 Rectangular Matrix with Ones on the Main Diagonal:

rectangular_matrix = np.eye(4, 5)
print(rectangular_matrix)

Output:

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

Example 3: Shifting the Diagonal using the k Parameter:

shifted_diagonal = np.eye(3, k=1)
print(shifted_diagonal)

shifted_diagonal_negative = np.eye(3, k=-1)
print(shifted_diagonal_negative)

Output:

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

Example 4: Specifying the Data Type:

integer_identity = np.eye(2, dtype=int)
print(integer_identity)

Output:

[[1 0]
 [0 1]]

These examples demonstrate the versatility of numpy.eye in generating various matrix forms tailored to specific needs. Understanding these parameters empowers you to use this function effectively in your NumPy-based projects.