Python Pool: 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.

Contents of Tutorial


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.


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.


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.


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.


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)


matrix a=


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
print("matrix b=n",b)


matrix b=


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


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.

Author: Shantun Parmar

11 thoughts on “Python Pool: NumPy Eye Explained With In-Depth Examples in Python

  1. I am only writing to make you understand what a really good experience my wife’s princess obtained viewing your webblog. She noticed some things, which included what it is like to possess an ideal teaching spirit to make others without difficulty learn about specific tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those productive, trusted, informative as well as unique tips on your topic to Mary.

  2. My wife and i were so relieved when Michael managed to finish up his studies via the precious recommendations he obtained using your web site. It is now and again perplexing to simply happen to be offering things which often some people might have been trying to sell. We remember we now have you to thank for this. The type of illustrations you made, the straightforward website navigation, the relationships your site make it easier to promote – it is mostly superb, and it is letting our son in addition to us recognize that this subject matter is pleasurable, which is certainly truly serious. Thanks for the whole thing!

  3. I enjoy you because of all of the effort on this blog. Debby delights in conducting internet research and it’s easy to see why. A number of us hear all about the compelling form you create both useful and interesting guides by means of the blog and boost contribution from other ones about this area of interest and my child has been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a fabulous job.

Thanks for your support, You may click on ads to encourage us which assits to writers.

Leave a Reply

Your email address will not be published.