NumPy eye creates a matrix with ones on one diagonal and zeros elsewhere

A diagonal pattern can be constructed directly, without editing individual cells. NumPy eye creates a two-dimensional array with ones on a chosen diagonal and zeros in the remaining positions. Academia Master Trader presents this English lesson for readers in Burundi, using constructed matrices to explain row counts, column counts, offsets and data types.

One size produces a square matrix

After import numpy as np, the call np.eye(3,dtype=int) returns [[1,0,0],[0,1,0],[0,0,1]]. It creates three rows and, because no separate column count is supplied, three columns.

The main diagonal contains positions whose row and column indices match. Every other position contains zero. This square, main-diagonal pattern is an identity matrix. The output has shape (3,3).

Eye constructs a new pattern from size and diagonal parameters. It does not extract entries from an existing matrix or inspect a supplied collection of values. To build a diagonal from specific numbers rather than ones, another construction is needed.

NumPy eye rectangular pattern

A separate column count allows a rectangular result

The call np.eye(3,4,dtype=int) returns [[1,0,0,0],[0,1,0,0],[0,0,1,0]]. The first argument is the row count, and the second is the column count. The shape is therefore (3,4).

The fourth column does not gain a one merely because it exists. Only positions on the selected diagonal receive ones. A rectangular output is a diagonal pattern, but it is not a square identity matrix.

Write the intended shape before describing the resulting entries. Reversing the two size arguments creates a different rectangular array, even though both calls still place ones on their main diagonals.

The offset moves the diagonal, not the array boundary

The call np.eye(3,4,k=1,dtype=int) returns [[0,1,0,0],[0,0,1,0],[0,0,0,1]]. Positive k selects an upper diagonal. Here the ones start in column one of row zero.

The call np.eye(3,4,k=-1,dtype=int) returns [[0,0,0,0],[1,0,0,0],[0,1,0,0]]. Negative k selects a lower diagonal. The first row contains no one because that diagonal begins below it.

Both outputs retain shape (3,4). Changing k does not add rows, crop columns or wrap a one around an edge. The selected diagonal simply occupies the positions that fit within the requested rectangle.

Data type and memory order describe different choices

The examples request dtype=int so the printed entries are integers. Eye also accepts other data types; for example, dtype=bool produces true entries on the chosen diagonal and false entries elsewhere. The geometric pattern remains the same.

The order parameter chooses row-major or column-major storage in memory. It does not transpose the visible matrix or change which diagonal receives ones. Keep storage layout separate from the array's row and column interpretation.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the row count, column count, offset and data type beside each constructed diagonal pattern.