numpy.eye() in Python
×


numpy.eye() in Python

138

NumPy's eye() function is a versatile tool for creating 2D arrays with ones on the diagonal and zeros elsewhere. It's particularly useful for generating identity matrices, which are fundamental in linear algebra and various numerical computations.

Syntax of numpy.eye()

The syntax for numpy.eye() is as follows:

numpy.eye(N, M=None, k=0, dtype=, order='C', like=None)

Parameters:

  • N: Number of rows in the output array.
  • M: Number of columns in the output array. If None, defaults to N, creating a square matrix.
  • k: Index of the diagonal. 0 (default) refers to the main diagonal, positive values refer to upper diagonals, and negative values to lower diagonals.
  • dtype: Desired data-type for the returned array. Default is float.
  • order: Whether to store the array in row-major (C-style) or column-major (Fortran-style) order in memory. Default is 'C'.
  • like: Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it.

Creating a Basic Identity Matrix

To create a 3x3 identity matrix, you can use the following code:

import numpy as np
arr = np.eye(3)
print(arr)

Output:

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

This creates a square matrix with ones on the main diagonal and zeros elsewhere.

Creating a Rectangular Matrix

To create a rectangular matrix, specify different values for N and M:

arr = np.eye(3, 5)
print(arr)

Output:

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

This creates a 3x5 matrix with ones on the main diagonal and zeros elsewhere.

Shifting the Diagonal

To shift the diagonal, use the k parameter:

arr = np.eye(4, k=1)
print(arr)

Output:

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

Setting k=1 shifts the diagonal one position above the main diagonal.

Specifying the Data Type

To specify the data type of the returned array, use the dtype parameter:

arr = np.eye(3, dtype=int)
print(arr)

Output:

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

This creates a 3x3 identity matrix with integer values.

Use Cases of numpy.eye()

NumPy's eye() function is widely used in various applications:

  • Linear Algebra: Creating identity matrices for matrix operations.
  • Simulations: Generating matrices for simulations in physics and engineering.
  • Machine Learning: Initializing weights in neural networks.
  • Control Systems: Designing systems with specific state-space representations.

Conclusion

Understanding how to use numpy.eye() effectively allows you to generate matrices with ones on the diagonal and zeros elsewhere, providing a foundation for various numerical computations and data processing tasks. By specifying the size, shape, and data type of the matrix, you can tailor the function to suit your specific needs.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat