Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.eye

Creates a 2-D array containing ones on a chosen diagonal and zeros elsewhere. The result can be square or rectangular, with configurable diagonal offset, dtype, memory order, and array-creation dispatch.

How it works

numpy.eye allocates a two-dimensional array of the requested shape, initializes zeros everywhere, and places ones along the diagonal selected by k. If M is omitted, the output is square; dtype, memory order, and optional array-creation dispatch can be specified.

Syntax

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

Parameters

NameTypeDescription
NintNumber of rows in the output.
Mint, optionalNumber of columns in the output. If None, it defaults to N.
kint, optionalDiagonal offset: zero is the main diagonal; positive values select upper diagonals and negative values select lower diagonals.
dtypedata-type, optionalData type of the returned array.
order{'C', 'F'}, optionalMemory layout: 'C' for row-major or 'F' for column-major.
devicedevice, optionalReference object that enables dispatch to an alternative array implementation.
likearray_like, optionalReference object allowing creation through an alternative array implementation.

Returns

An ndarray with shape (N, M) and ones on the selected diagonal; when M is omitted, the shape is (N, N).

Key points

  • The function produces a two-dimensional numerical array whose selected diagonal entries are one and all other entries are zero.
  • N controls how many rows the returned array has.
  • M controls the column count when a non-square result is needed.
  • k=0 selects the main diagonal, while other integer offsets select diagonals relative to it.
  • Use dtype to choose the element type, for example int when integer ones and zeros are preferred.
  • The order argument controls whether the result uses C-style or Fortran-style storage.

Common mistakes

  • Omitting M makes the number of columns equal to N; it does not create a separately inferred rectangular shape.
  • Positive and negative k values move the one-valued diagonal above and below the main diagonal, respectively.
  • order changes the array's memory layout, not the visible diagonal pattern.

Example: Identity and offset-diagonal arrays

import numpy as np

identity = np.eye(3, dtype=int)
offset = np.eye(3, 4, k=1)

print(identity)
print(offset)

Creates a 3×3 integer identity array and a 3×4 integer-default floating-point array with ones on the first upper diagonal.

When to use it

Identity matrices

Initialize linear-algebra operations that require an identity-shaped starting matrix.

Offset and rectangular patterns

Build rectangular masks or band-aligned structures by shifting the one-valued diagonal.

Controlled array representation

Request a specific numeric representation or storage layout for downstream computation.

Interview questions

QWhat does np.eye create?

It returns a two-dimensional array with ones on the selected diagonal and zeros elsewhere. With the default k=0, the selected diagonal is the main diagonal.

Follow-up: How would you create a rectangular identity-like array? Pass both dimensions, such as np.eye(3, 5), leaving k at zero unless an offset is required.

QHow do N and M determine the result shape?

N specifies the row count, while optional M specifies the column count and defaults to N. Therefore, np.eye(3) is square, whereas np.eye(3, 5) has three rows and five columns.

Follow-up: What happens when the offset exceeds the available diagonals? The resulting array contains no ones on that requested diagonal because it lies outside the shape.

QHow do k and dtype affect the output?

k selects the diagonal: zero is the main diagonal, positive values select upper diagonals, and negative values select lower diagonals. The default output dtype is floating point unless another dtype is supplied.

Follow-up: When would order='F' matter? It requests Fortran-style column-major storage, which can benefit consumers expecting that memory layout without changing array values.

  • numpy.identity
  • numpy.diag
  • numpy.tri
  • numpy.matlib.eye

Source: numpy.eye. Checked 2026-10-15.