NumPy Eye Explained With In-Depth Examples in Python

Hello geeks and welcome in today’s article, we will discuss NumPy eye(). Along with it, we will also cover its parameter, syntax, and a couple of examples. In general, NumPy is a numerical module of python which provides a function eye. Now NumPy.eye() returns a 2-d array with 1’s at diagonal and 0’s elsewhere. Somewhat similar to an identity matrix. In this case, the diagonal can be upper, lower, or middle, depending on the value of k. Here k is one of the parameters that we will cover as we move ahead in this article.

SYNTAX OF NUMPY EYE

In this section we will discuss the syntax for NumPy eye()

numpy.eye(NM=Nonek=0dtype=<class 'float'>order='C') (as per V1.19)

Above, we can see the general syntax of NumPy eye(). However, in the newer version that is V1.21, there is minute addition to the syntax that we will see next.

numpy.eye(NM=Nonek=0dtype=<class 'float'>order='C'*like=None) (as per V1.21)

From above, we get a rough understanding of the NumPy.eye(). In the next section, we will look at the different parameters present in the syntax.

PARAMETERS OF NUMPY EYE()

In this section we will cover the different parameters present in the syntax. In general, if we combine the 2 versions there are a total of 6 parameters.

n: int

This parameter represents the number of rows in the output.

m:int

This is an optional parameter that represents the number of columns in the output. By default it is equal to the number of rows that is N.

K:int

This is another optional parameter that refers to the index of the diagonal. The value of k determines whether the diagonal will an upper diagonal or a lower diagonal.

  • For k>0: upper diagonal
  • k=0 : main diagonal (default value)
  • k<0: lower diagonal

DTYPE: data type

An optional parameter represents data-type of returned array.

ORDER

This parameter takes care of whether the output to be sorted in row-major(c-type) or column-major(F-style).

LIKE: array_like

The new addition to the version 1.21. This parameter allows the creation of arrays which are not NumPy arrays.

Please Note*- The like keyword is experimental feature yet waiting for approval.

RETURN Type of Numpy Eye

Ndarray of shape(M, N)
An array whose all elements are equal to 0, except the kth diagonal whose values are equal to 1.

EXAMPLES of Numpy Eye

Now let us look at certain examples to get a better understanding of NumPy.eye()

import numpy as ppool
a=ppool.eye(2, dtype=int)
print("matrix a=\n",a)

Output:

matrix a=
[[1,0]
 [0,1]]

EXPLANATION

In the above example, we have used the eye(). To get a 2*2 matrix with all the non-diagonal terms equal to equal to 0. Here we have not defined the value of K, so by default, it is considered 0. That’s why we get a main diagonal structure.

Now let us consider another example were the value of K is other then 0.

import numpy as ppool
b=ppool.eye(4,k=1)
print("matrix b=\n",b)

Output:

matrix b=
[[0.,1.,0.,0.]
 [0.,0.,1.,0.]
 [0.,0.,.0,1.]
 [0.,0.,0.,0.,]]

EXPLANATION

In the above example, we generated a 4*4 matrix with the help of NumPy.eye(). Here since we have specified the value of k to be =1 we get an upper diagonal structure. Another key point to emphasize is that here since we have not declared the datatype, we get a float datatype. So float is the by-default data-type of this function.

Must Read

CONCLUSION

In this article, we covered the eye() function. For better understanding, we looked at its syntax, parameter as well as a couple of examples. Finally, we can conclude that NumPy.eye() is used to print 2-d arrays with all the non-diagonal terms equal to 0. I hope this article was able to your doubts. If you have any more doubts, feel free to write them below in the comment section. Done with this, why not study numpy squeeze next.

5 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments