numpy.indices()函数– Python
numpy.indices()
函数返回一个表示网格索引的数组。计算一个数组,其中子数组包含索引值 0, 1, ... 仅沿相应轴变化。
Syntax : numpy.indices(dimensions, dtype, sparse = False)
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
代码#1:
# Python program explaining
# numpy.indices() function
# importing numpy as geek
import numpy as geek
gfg = geek.indices((2, 3))
print (gfg)
输出 :
[[[0 0 0]
[1 1 1]]
[[0 1 2]
[0 1 2]]]
代码#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)
输出 :
[[0 1 2]
[0 1 2]]