NumPy np.eye()

The eye function in NumPy allows you to generate a two-dimensional array comprised of ones on the diagonal and zeros elsewhere.

Let us discuss what the eye function entails and how to use it.

NumPy np.eye Syntax

The function syntax is as follows:

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

The parameters are defined according to their following functions:

  1. N – represents the number of rows in the output array.
  2. M – represents the number of columns in the output array. This is an optional parameter of int type. If not specified, it defaults to the value of N.
  3. k – denotes the index of the diagonal. The value is set to 0, meaning the main diagonal. If the value of K is a positive integer, it means an upper diagonal from the main. If the vaulue of K is negative, it means the lower diagonal from the main.
  4. dtype – represents the data type of the resulting array.
  5. order – specifies whether the output array should be sorted in row-major or column-major. Accepted parameter values are ‘C’ and ‘F’ for row-major and column-major, respectively.
  6. like – an array prototype or array_like object.

Return Value

The function will return an array where all the elements in the array are equal to zero except the elements in the diagonal specified by the k parameter, which are equivalent to one.

Np.eye() Main Diagonal

The following example illustrates how to use the np.eye() function with the main diagonal:

# import numpy
import numpy as np
arr = np.eye(3, k=0);

The previous code creates a 2D array where the elements in the main diagonal are equal to 1.

This results in the following array:

array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

NOTE: The array is comprised of three rows specified in the first function parameter.

Np.eye() Upper Diagonal

To use an upper diagonal, set the k parameter to a positive integer, as shown in the example:

arr = np.eye(3, k=1);

In this case, we set the upper diagonal to index one from the main diagonal. The code above results in the following array:

array([[0., 1., 0.],
       [0., 0., 1.],
       [0., 0., 0.]])

Np.eye() Lower Diagonal

Similarly, we can specify a lower diagonal by specifying the value as a negative integer. Consider the example below:

arr = np.eye(3, k=-1);

The above example returns to the following array:

array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 1., 0.]])

Np.eye() Float Data Type

By default, the np.eye() function returns the integer types. However, we can specify the target data type using the dtype parameter as follows:

arr = np.eye(3, k=-1, dtype=float);

The resulting array is as follows:

array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 1., 0.]])


This article explored how to use the NumPy eye function to generate a 2D array populated with zeros except the specified diagonal. Browse through Linux Hint website for more tips and tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list