numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
indices(dimensions, dtype=<class 'int'>, sparse=False)

Compute an array where the subarrays contain index values 0, 1, ... varying only along the corresponding axis.

Notes

The output shape in the dense case is obtained by prepending the number of dimensions in front of the tuple of dimensions, i.e. if dimensions is a tuple (r0, ..., rN-1) of length N , the output shape is (N, r0, ..., rN-1) .

The subarrays grid[k] contains the N-D array of indices along the k-th axis. Explicitly:

grid[k, i0, i1, ..., iN-1] = ik

Parameters

dimensions : sequence of ints

The shape of the grid.

dtype : dtype, optional

Data type of the result.

sparse : boolean, optional

Return a sparse representation of the grid instead of a dense representation. Default is False.

versionadded

Returns

grid : one ndarray or tuple of ndarrays

If sparse is False:

Returns one array of grid indices, grid.shape = (len(dimensions),) + tuple(dimensions) .

If sparse is True:

Returns a tuple of arrays, with grid[i].shape = (1, ..., 1, dimensions[i], 1, ..., 1) with dimensions[i] in the ith place

Return an array representing the indices of a grid.

See Also

meshgrid
mgrid
ogrid

Examples

>>> grid = np.indices((2, 3))
... grid.shape (2, 2, 3)
>>> grid[0]        # row indices
array([[0, 0, 0],
       [1, 1, 1]])
>>> grid[1]        # column indices
array([[0, 1, 2],
       [0, 1, 2]])

The indices can be used as an index into an array.

>>> x = np.arange(20).reshape(5, 4)
... row, col = np.indices((2, 3))
... x[row, col] array([[0, 1, 2], [4, 5, 6]])

Note that it would be more straightforward in the above example to extract the required elements directly with x[:2, :3] .

If sparse is set to true, the grid will be returned in a sparse representation.

>>> i, j = np.indices((2, 3), sparse=True)
... i.shape (2, 1)
>>> j.shape
(1, 3)
>>> i        # row indices
array([[0],
       [1]])
>>> j        # column indices
array([[0, 1, 2]])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.take skimage.segmentation._watershed.watershed numpy.ma.core.MaskedArray.put numpy.fromfunction numpy.put_along_axis

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/core/numeric.py#1680
type: <class 'function'>
Commit: