Delving into Array Creation Routines: NumPy.eye() Explained
Function arguments
numpy.eye()
takes a few optional arguments that control the size and properties of the resulting identity matrix:N (int)
: This is the primary argument, specifying the number of rows and columns in the identity matrix. If unspecified, it defaults to 1.M (int, optional)
: This argument allows you to define a different number of columns compared to rows. By default, it's set toN
.k (int, optional)
: This argument specifies the index of the diagonal to set to ones. The default diagonal is 0 (the main diagonal).dtype (dtype, optional)
: This argument allows you to specify the data type of the elements in the identity matrix. The default data type is float64.order (str, optional)
: This argument controls the memory layout of the created array. It's typically left as the default ('C') for row-major order.device (object, optional)
: This argument is used for specifying the device memory on which the array is allocated (relevant for GPU arrays).like (array_like, optional)
: This argument allows you to create an identity matrix with the same shape and data type as another array.
Array creation routines
NumPy offers a set of functions for creating arrays with different properties. These functions fall under the category of "array creation routines."numpy.eye()
is one such function specifically designed to generate identity matrices.
import numpy as np
# Create a 3x3 identity matrix
identity_matrix = np.eye(3)
# Print the identity matrix
print(identity_matrix)
This code will output:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
As you can see, the resulting array is a 3x3 identity matrix with 1s on the diagonal and 0s elsewhere.
Creating a 2x2 identity matrix with integer elements
import numpy as np
# Create a 2x2 identity matrix with integer data type
identity_matrix = np.eye(2, dtype=int)
print(identity_matrix)
[[1 0]
[0 1]]
Creating a 4x3 identity matrix with 1s on the diagonal below the main diagonal (k=-1)
import numpy as np
# Create a 4x3 identity matrix with 1s on the lower diagonal
identity_matrix = np.eye(4, 3, k=-1)
print(identity_matrix)
[[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Creating a 5x5 identity matrix using another array as a reference for shape and data type
import numpy as np
# Sample array
sample_array = np.random.rand(3, 4) # Create a random 3x4 array
# Create a 5x5 identity matrix with the same shape and data type as sample_array
identity_matrix = np.eye(*sample_array.shape, like=sample_array)
print(identity_matrix)
This code first creates a random array sample_array
. Then, it uses np.eye()
with the unpacked shape (*sample_array.shape
) from sample_array
and the like
argument referencing sample_array
to generate a 5x5 identity matrix with the same data type as the random array.
List comprehension
import numpy as np
def create_identity_matrix(n):
"""
Creates an identity matrix of size nxn using list comprehension.
"""
identity_matrix = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
return np.array(identity_matrix)
# Example usage
identity_matrix = create_identity_matrix(4)
print(identity_matrix)
This approach is less efficient than numpy.eye()
for larger matrices, but it can be useful for understanding the underlying structure of identity matrices.
NumPy functions for element-wise operations
import numpy as np
def create_identity_matrix(n):
"""
Creates an identity matrix of size nxn using NumPy operations.
"""
identity_matrix = np.zeros((n, n))
identity_matrix[np.diag_indices(n)] = 1
return identity_matrix
# Example usage
identity_matrix = create_identity_matrix(4)
print(identity_matrix)
This approach is less readable and potentially slower than numpy.eye()
for larger matrices.
- List comprehension and element-wise operations with NumPy functions can be used as alternatives for didactic purposes or specific use cases, but they are generally less efficient.
numpy.eye()
is the preferred and most efficient method for creating identity matrices in NumPy.