Note that the output from indexing operations can have different shape from the
original object. To preserve the original dimensions after indexing, you can
use newaxis. To use other such tools, refer to
Dimensional indexing tools.
To get the indices of each maximum or minimum value for each
(N-1)-dimensional array in an N-dimensional array, use reshape
to reshape the array to a 2D array, apply argmax or argmin
along axis=1 and use unravel_index to recover the index of the
values per slice:
>>> x=np.arange(2*2*3).reshape(2,2,3)%7# 3D example array>>> xarray([[[0, 1, 2], [3, 4, 5]], [[6, 0, 1], [2, 3, 4]]])>>> x_2d=np.reshape
The first array returned contains the indices along axis 1 in the original
array, the second array contains the indices along axis 2. The highest
value in x[0] is therefore x[0,1,2].
Index the same ndarray multiple times efficiently
It must be kept in mind that basic indexing produces views
and advanced indexing produces copies, which are
computationally less efficient. Hence, you should take care to use basic
indexing wherever possible instead of advanced indexing.