NumPy eye creates a two-dimensional array with ones on a chosen diagonal and zeros elsewhere

A diagonal pattern can be created directly from dimensions rather than extracted from an existing matrix. NumPy eye builds a two-dimensional array whose chosen diagonal contains ones and whose remaining positions contain zeros. The row count, column count and diagonal offset describe the structure before any values from an external dataset are involved.

Academia Master Trader presents this explanation in English for readers in Namibia. The examples concern numerical arrays and their arrangement only. They do not represent prices, trading signals or financial results. The aim is to understand what each size parameter controls and why a diagonal array need not always be square.

Begin with a square arrangement

The expression np.eye(3,dtype=int) creates rows [1,0,0], [0,1,0] and [0,0,1]. Its shape is (3,3). The first argument specifies three rows. Because the column count is omitted, it defaults to the same number, giving three columns as well.

The ones occupy the main diagonal. With zero-based indices, these are coordinates (0,0), (1,1) and (2,2). Every other coordinate contains zero. The integer data type in this example makes the printed values appear as integers rather than floating-point values.

Original diagram: a three-by-three array has ones at equal row and column indices, with zeros in every other position.

The function does not read those ones from another array. It creates the stated pattern. Nor does the number three become an entry repeated throughout the output: it sets a dimension length. Separating size arguments from array contents is the first step towards reading the expression correctly.

Rows and columns can have different lengths

The optional argument M specifies the number of columns. For example, np.eye(2,3,dtype=int) returns rows [1,0,0] and [0,1,0], with shape (2,3). The first number sets two rows; the second sets three columns.

This rectangle has two main-diagonal ones. There is no third row in which to place another one, even though a third column exists. The unused positions remain zero. The diagonal continues only as far as the available row and column positions permit.

Likewise, np.eye(3,2,dtype=int) produces rows [1,0], [0,1] and [0,0]. Its shape is (3,2). Swapping the two size arguments therefore changes the layout. Writing the expected shape next to an expression makes a row-count and column-count mix-up easier to notice.

Choose another diagonal with k

The parameter k selects the diagonal containing ones. Zero is the default and means the main diagonal. A positive value refers to a diagonal above it. The expression np.eye(3,k=1,dtype=int) produces rows [0,1,0], [0,0,1] and [0,0,0].

Here the ones occupy coordinates (0,1) and (1,2). The offset changes their positions, not their numeric value. It also does not add a column or row: the requested shape remains (3,3). The last row contains only zeros because the selected upper diagonal has no position in that row.

A negative offset selects a lower diagonal. With np.eye(3,k=-1,dtype=int), the rows are [0,0,0], [1,0,0] and [0,1,0]. The ones now occupy (1,0) and (2,1). The sign of k describes the diagonal's location, not negative entries in the resulting array.

A familiar special case has a precise boundary

The square arrangement with ones on the main diagonal and zeros elsewhere is the identity matrix. Eye can create that arrangement, but it can also create rectangles and shifted diagonal patterns. Calling every possible output an identity matrix would ignore those additional choices.

The shape and the offset should therefore accompany the name. A three-by-three main-diagonal result and a two-by-three result are both valid eye outputs, but they are not the same mathematical object. The function name alone does not settle the meaning of every array it can construct.

This also differs from constructing a diagonal matrix from a vector of supplied values. A function that places [2,5,8] on a diagonal preserves those supplied values. Eye instead uses ones for the selected diagonal. Decide which content the program needs before choosing the construction.

Data type and storage order are separate choices

The data-type parameter controls the type of the output entries. This lesson uses dtype=int so that the examples display zero and one as integers. Choosing a data type does not change which coordinates belong to the selected diagonal.

The order parameter concerns memory layout: C-style row-major storage or Fortran-style column-major storage. It does not mean that the diagonal moves from one side of the matrix to the other. The visible coordinate pattern is described by the dimensions and k, independently of that storage-order choice.

For introductory work, keep these questions separate: what shape is required, which diagonal contains ones and which data type suits the later calculation? Further options should be considered only when the program needs them. Adding parameters without explaining their role can obscure a simple construction.

Explain the structure before using it

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. State the row count, column count and diagonal offset, then identify one position that contains one and another that contains zero. This turns a compact array expression into a clear description of the intended structure.