Related Articles

# numpy.indices() function – Python

• Last Updated : 11 Jun, 2020

`numpy.indices()` function return an array representing the indices of a grid. Compute an array where the subarrays contain index values 0, 1, … varying only along the corresponding axis.

Syntax : numpy.indices(dimensions, dtype, sparse = False)

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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.

Return : [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

Code #1 :

 `# Python program explaining``# numpy.indices() function``      ` `# importing numpy as geek ``import` `numpy as geek ``  ` `gfg ``=` `geek.indices((``2``, ``3``))`` ` `print` `(gfg)`

Output :

```[[[0 0 0]
[1 1 1]]

[[0 1 2]
[0 1 2]]]
```

Code #2 :

 `# Python program explaining``# numpy.indices() function``      ` `# importing numpy as geek ``import` `numpy as geek ``  ` `grid ``=` `geek.indices((``2``, ``3``))``gfg ``=` `grid[``1``]`` ` `print` `(gfg)`

Output :

```[[0 1 2]
[0 1 2]]
```

My Personal Notes arrow_drop_up