NumPy indices builds coordinate grids whose leading axis identifies each dimension

An array's values and its coordinates answer different questions. NumPy indices constructs coordinate arrays for a requested grid shape. Academia Master Trader presents this English lesson for readers in the Republic of the Congo, using a small rectangular grid to explain row coordinates, column coordinates, dense output and sparse coordinate representations.

A two-dimensional grid needs two coordinate arrays

After import numpy as np, define grid = np.indices((2,3)). The requested grid has two rows and three columns. The returned dense array has shape (2,2,3).

The extra leading dimension has length two because there are two coordinate directions. grid[0] contains row indices: [[0,0,0],[1,1,1]]. grid[1] contains column indices: [[0,1,2],[0,1,2]].

The row coordinate stays constant across each row and changes when moving downwards. The column coordinate changes when moving across a row and repeats in the next row. These are positions, not measurements stored in a separate dataset.

NumPy indices coordinate grids

The leading axis identifies the coordinate direction

At row one, column two, grid[0,1,2] is one and grid[1,1,2] is two. Together they describe the coordinate pair (1,2).

Do not interpret the leading axis as another spatial dimension of the requested rectangle. It selects which coordinate component is being read. The requested grid remains two-dimensional even though the dense result is a three-dimensional array.

More generally, a requested shape with N dimensions produces dense output whose shape begins with N, followed by the requested dimensions. Each subarray supplies coordinates along one corresponding axis.

Sparse output keeps repeated coordinates implicit

The assignment rows, cols = np.indices((2,3),sparse=True) returns a tuple of coordinate arrays. Here rows is [[0],[1]] with shape (2,1), while cols is [[0,1,2]] with shape (1,3).

These are ordinary arrays with singleton dimensions, not a compressed sparse matrix format. Their shapes allow broadcasting to combine the coordinate directions without first storing both complete dense grids.

For example, 10*rows + cols produces [[0,1,2],[10,11,12]]. The result has shape (2,3) because broadcasting combines the two coordinate directions. The expression is a constructed coordinate label, not a formula for interpreting every dataset.

Coordinate grids can describe a positional condition

The expression rows == cols produces [[True,False,False],[False,True,False]]. It identifies matching row and column indices within the rectangle.

This condition depends only on coordinates. It does not inspect numeric values from an existing matrix. To use a coordinate condition with data, first establish that the data shape and the grid shape refer to the same axes.

Indices accepts dtype to choose the coordinate array data type. Keep that storage choice separate from the requested grid dimensions and the meaning assigned to each coordinate direction.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the requested shape, dense or sparse representation and coordinate-axis order before using a grid in another expression.