NumPy eye function: A Complete Guide

Numpy Eye

NumPy is a popular Python library for scientific computing that provides efficient operations on arrays and matrices. One of the useful functions in NumPy is the eye function, which allows users to create an identity matrix of a specific size. An identity matrix is a square matrix with ones on the main diagonal and zeros everywhere else.

In this article, we will explore the eye function in detail, including its syntax, parameters, and examples of how to use it in various scenarios.

Whether you are new to NumPy or an experienced user, this article will provide a comprehensive guide to using the eye function.

What is numpy.eye()?

Numpy.eye() is used to return a two-dimensional (2-D) array with 1s along the diagonal and 0s everywhere else. The optional parameter k determines whether the diagonal is main, upper, or lower. The main diagonal is designated by 0 k (default), the upper diagonal by a positive k, and the lower diagonal by a negative k.


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


  • N: int
    • Required
    • The number of rows in the output array.
  • M: int
    • Optional
    • The number of columns in the output array, if None, then by default set to N, hence forming a symmetric matrix.
  • k: int
    • Optional
    • The main diagonal is indicated by the index value of 0, an upper diagonal by a positive value, and a lower diagonal by a negative value.
  • dtype: data-type
    • Optional
    • The data type of the returned array.
  • order: {‘C’, ‘F’}
    • Optional
    • Which order—row-major (C-style) or column-major (Fortran-style)—should be used to store the output in memory?
  • like: array_like
    • Optional
    • Arrays that are not NumPy arrays can be created using a reference object. The outcome will be determined by an array-like provided in as like if it complies with the array function protocol. In this instance, it makes sure that an array object is created that is compatible with the one that was provided as an argument.

Implementation of Numpy.eye()

Make sure to import the NumPy package in your IDE before using this function. To import the NumPy package run the following line of code.

import numpy as np

Example 1: Passing only the ‘n’ parameter, hence forming an identity array

Example 1
Example 1

Example 2: Passing ‘n’ and ‘m’ parameters

Example 2
Example 2

Example 3: Passing the ‘k’ parameter

# k = 2, diagonal 2 steps forward the default diagonal 

# k = -1,diagonal 1 step downard the default diagonal
Example 3
Example 3


NumPy eye is a useful method for creating identity matrices in scientific computing. It is simple to use and has a variety of parameters that allow users to customize the size and type of the matrix.

By understanding the syntax and parameters of the eye function, as well as some common pitfalls and tips for using it, you can confidently and effectively use the eye function in your own projects.

Whether you are working with matrices in machine learning, linear algebra, or any other field, the eye function can be a valuable addition to your NumPy toolkit. So, it is a very important function in NumPy library.