NumPy identity creates a square array whose main diagonal is one and whose other entries are zero

A square numerical array can have a simple and precise pattern: one on every position of the main diagonal and zero everywhere else. NumPy identity constructs that pattern from a single size argument. The same number determines both the row count and the column count.

Academia Master Trader presents this explanation in English for readers in Liberia. The examples concern numerical arrays only. They are not prices, trading signals or financial results. The aim is to read the size argument, recognise the main diagonal and distinguish an identity array from an array filled entirely with ones.

One size determines two dimensions

The expression np.identity(3) creates an array with shape (3,3). It has three rows and three columns, giving nine positions in total. The size argument is three, not nine: it specifies the side length of the square rather than its total entry count.

Reading the rows gives [1.,0.,0.], [0.,1.,0.] and [0.,0.,1.]. The ones move one column to the right as the row moves one step down. All positions outside those three main-diagonal positions contain zero.

Original diagram: a three-by-three identity array has ones on the main diagonal and zeros in the other six positions.

This example separates three quantities that are easy to confuse: three is the side length, nine is the total entry count and three is the number of diagonal ones. The equal side length and diagonal count follow from the square pattern; they do not make the output one-dimensional.

Identify the diagonal by coordinates

The main diagonal contains positions whose row and column coordinates match. With zero-based indexing in the three-by-three example, those positions are (0,0), (1,1) and (2,2). Each of them contains one.

The position (0,2) is not on that diagonal because its row and column coordinates differ. Its value is zero. The same is true of (2,0). Looking at both corners helps distinguish the main diagonal from the other diagonal across the square.

A diagonal is a set of positions, not a separate row stored outside the array. Each highlighted position still belongs to an ordinary row and column. The diagram uses colour to draw attention to the pattern, while the actual numerical result consists of the ones and zeros themselves.

An identity array is not filled entirely with one

An array of ones places one in every requested position. Identity instead places one only on the main diagonal, with zero in the remaining positions. In the three-by-three case, that means three ones and six zeros, not nine ones.

The distinction remains important even when both arrays have the same shape. Shape tells you how entries are arranged; it does not tell you what those entries contain. Two arrays can both be three-by-three while representing very different numerical patterns.

Describe the pattern explicitly rather than calling any square array an identity array. Being square is necessary for this constructor's result, but it is not the complete description. The diagonal values and the values outside the diagonal are also part of the definition.

The output is square by construction

Identity takes a single size n for an n-by-n result. It does not take separate row and column lengths in this interface. Supplying three asks for both dimensions to have length three, not for a vector containing three positions.

For a size of two, the rows are [1.,0.] and [0.,1.]. The shape is (2,2). This smaller example preserves the same rule: equal row and column counts, main-diagonal ones and zeros outside that diagonal.

A size of one produces a one-by-one array containing one. It is still a two-dimensional square arrangement, even though it contains just one value. Counting visible numbers alone does not determine the number of axes.

Data type is separate from the pattern

The official reference lists a floating-point default for the output. The printed example therefore uses values such as 1. and 0.. Those values express the diagonal pattern in floating-point form.

The expression np.identity(3,dtype=int) requests integer entries instead. The three-by-three shape and the locations of the ones remain the same. Changing the requested representation is not the same as moving the diagonal or changing the side length.

If a later calculation requires a particular numerical type, make that requirement clear at creation. A short explanation should state both the intended pattern and the intended representation. That gives the reader two separate facts instead of asking them to infer type from appearance alone.

Construction does not inspect another array

The documented function returns the requested identity array. Its size argument describes the output to create. It does not select entries from an existing matrix or analyse another array to decide whether its diagonal already has the desired values.

Use precise wording when explaining a program. Creating an identity array, extracting a diagonal and checking the contents of an existing matrix are different tasks. This constructor addresses the first of them: it creates the known square pattern directly.

Read the complete pattern before using it

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. State the side length, confirm the square shape, identify the matching row and column coordinates and describe the zeros elsewhere. This gives a complete account of the array before any later calculation uses it.