Updated May 26, 2023
Introduction to NumPy.eye()
Python programmers typically utilize the Numpy.eye() function. This function enables the system to return an output array where all values, except for the kth diagonal, are set to zero. The kth diagonal is assigned a value of 1. The user can specify the diagonal for which the value of 1 will be assigned when using this function.
Syntax and Parameters
The syntax used to utilize the numpy.eye() function while writing codes in the Python programming language is as follows:
numpy.eye(* N *, * M * = * None *, * k * = * 0 *, * dtype * = * < * class * ' * float * ' * > *, * order * = * ' * C * ' *) *
The parameters used for the NumPy function eye() in the Python programming language are as follows:
Parameters used in the function  Description of the Parameter 
N * int  The parameter ‘N’, represents the total number of rows that are present in the array. * * * * * * * 
M * int, optional  The parameter ‘N’ represents the total number of columns in the array. The default value for the function is where the total number of columns and the total number of rows are equal. It is an optional parameter. * * * * * * * * * * * * * * * * * * * * * * 
k * int, optional  This parameter enables the representation of the main diagonal. The default value of k is 0. If the value is positive (i.e., k>0), it represents an upper diagonal, whereas when the value of k is negative, it represents a lower diagonal. The parameter is assumed to have a default value of zero. It is an optional parameter. * * * * * * * * * * * * * * * 
d * typedatatype, optional  It is an optional parameter. The parameter represents the data type of the returned array. The default value of the parameter is float datatype. 
order{‘C’, * ‘F’}, optional  It is an optional parameter. This can be assigned either of the two values C_ contiguous or F_ contiguous. This parameter suggests to the user the option of the orientation of the output. The user can determine whether the data should be stored in Cstyle (rowmajor style) or Fortranstyle (columnmajor style) to determine the order in which the data is placed in the system’s memory. 
Indarray of shape (N,M)

We apply the output array obtained from the numpy.eye() function to the input array.
The output array has all the elements represented as zero, except the kth element representing the diagonal value. The importance of the diagonal will be equal to one. 
How does the Numpy.eye function?
The system calculates the diagonal by identifying the positions of the elements on the main matrix. Then, the function replaces all values except for the diagonal elements with 0’s. The function replaces the elements at the diagonal indices with the value 1. The figure illustrates the replacement of the diagonal with 1, while the remaining elements in the matrix are replaced with the value 0.
Examples
Lets us discuss Examples of implementing eye function in NumPy.
Example #1
Code:
# importing the numpy class as np1
import numpy as np1
# declaration of a 2x2 matrix with 1 on its main diagonal
o1 = np1.eye(2, dtype = float)
print("The array which has been entered by the system : \n", o1)
# declaration of the array using the matrix with Row set at a value of 2 and Colum set at the value of 3 and the diagonal is equal to 1
o2 = np1.eye(2, 3, k = 1)
print("The array which has been entered by the system : \n", o2)
Output:
Example #2
Code:
# importing the numpy class as np1
import numpy as np1
# declaration of a 2x2 matrix with 1 on its main diagonal
b1 = np1.eye(2, dtype = float)
print("The Matrix B which has been entered by the system : \n :", b1)
# declaration of the array using the matrix with Row set at a value of 4 and Colum set at the value of 5 and the diagonal is equal to 1
a1 = np1.eye(4, 5, k = 1)
print("\n The Matrix A which has been entered by the system :\n", a1)
Output:
Conclusion
The eye function provides a predeveloped functionality that is quintessential and very handy as a preparatory step. The NumPy function eye() in Python is an inbuilt function that returns a resultant matrix, specifically a twodimensional array. In this matrix, we actively assign the value 1 to the diagonal section while assigning the value 0 to all other elements outside the diagonal. This function helps create multidimensional arrays and enables the implementation of various mathematical and statistical operations on the array. It facilitates the complete evaluation of the function’s output.
Recommended Articles
We hope that this EDUCBA information on “Numpy.eye()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.