Introduction to Numpy.eye()
The Numpy.eye() function is typically used in the Python coding language. With the help of the function the system is enabled to return the output array that all the values contained within the array are equal to zero with the exception of the kth diagonal, the value of which is equal to 1. the user has ability while using this function, to choose the diagonal which will be allocated the value of 1.
Syntax and Parameters
Following is the syntax which used to utilize the numpy.eye() while writing codes in the Python programming language:
numpy.eye(* N *, * M * = * None *, * k * = * 0 *, * dtype * = * < * class * ' * float * ' * > *, * order * = * ' * C * ' *) *
Following is the parameters used for the numpy *.eye() function written in the Python programming language:
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 that are present 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 is enabled to represents the main diagonal. The default value of k is 0. If the value is positive (i.e., k>0), it represents that it is an upper diagonal; whereas when the value of k is negative, it represents a lower diagonal. The default value for the parameter is assumed to be zero. It is an optional parameter. * * * * * * * * * * * * * * * |
d * typedata-type, optional | It is an optional parameter. The data type of the returned array is represented by the parameter. 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 the user the option of the orientation of the output. Whether it should be stored in the C-style (i.e., row-major style) or Fortran-style (i.e., column-major style) for the order of the data to placed in the memory of the system. |
Indarray of shape (N,M)
|
The output array after the function numpy.eye() is applied on the input array.
The output array has all the elements represented as zero with the exception of the k-th element representing the value of the diagonal. The values of the diagonal will be equal to one. |
How does the Numpy.eye function?
The diagonal is calculated by the system, identifying the position of the elements on the main matrix and then the function proceeds to replace the value except for the diagonal values by 0’s. The value of 1’s is replaced in all the elements spread across at the indices at the diagonal. The figure represents how the diagonal is replaced by 1st and the remaining elements in the matrix are replaced by the value zero.
Examples
Lets us discuss Examples to implement 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 for a pre-developed functionality which is quintessential and very handy acting as a preparatory step. Python’s function NumPy eye() is one of the in-built functions which is used in order to return a resultant matrix i.e., a two-dimensional array having the value of 1 in the diagonal section in the matrix and 0 placed at values in all other elements in the matrix (elsewhere with respect to the specific position on the kth value). This function helps in creating multiple multidimensional arrays and in turn, derives other mathematical and statistics related functions to be performed on the array which helps in resolving the complete value to the function.
Recommended Articles
This is a guide to Numpy.eye(). Here we discuss the introduction, parameters, How does the Numpy.eye function and examples respectively. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses