Identity matrices
Initialize linear-algebra operations that require an identity-shaped starting matrix.
NumPy · Function
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.
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.
numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C', *, device=None, like=None)| Name | Type | Description |
|---|---|---|
N | int | Number of rows in the output. |
M | int, optional | Number of columns in the output. If None, it defaults to N. |
k | int, optional | Diagonal offset: zero is the main diagonal; positive values select upper diagonals and negative values select lower diagonals. |
dtype | data-type, optional | Data type of the returned array. |
order | {'C', 'F'}, optional | Memory layout: 'C' for row-major or 'F' for column-major. |
device | device, optional | Reference object that enables dispatch to an alternative array implementation. |
like | array_like, optional | Reference object allowing creation through an alternative array implementation. |
An ndarray with shape (N, M) and ones on the selected diagonal; when M is omitted, the shape is (N, N).
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.dtype to choose the element type, for example int when integer ones and zeros are preferred.order argument controls whether the result uses C-style or Fortran-style storage.M makes the number of columns equal to N; it does not create a separately inferred rectangular shape.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.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.
Initialize linear-algebra operations that require an identity-shaped starting matrix.
Build rectangular masks or band-aligned structures by shifting the one-valued diagonal.
Request a specific numeric representation or storage layout for downstream computation.
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.
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.
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.identitynumpy.diagnumpy.trinumpy.matlib.eye