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 that provides a function eye. Now NumPy.eye() returns a 2-d array with 1’s at the 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(N, M=None, k=0, dtype=<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 a minute addition to the syntax that we will see next.

numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C', *, like=None) (as per V1.21)

From its syntax, 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.


This optional parameter 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 the data type of the returned array.


This parameter takes care of whether the output is 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 that 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.

Notify of
Inline Feedbacks
View all comments