Table Of Contents
Table Of Contents

NDArray.__getitem__

NDArray.__getitem__(key)[source]

x.__getitem__(i) <=> x[i]

Returns a sliced view of this array if the elements fetched are contiguous in memory; otherwise, returns a newly created NDArray. This functions supports advanced indexing defined in the following reference with some restrictions.

https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#combining-advanced-and-basic-indexing

  • If key is a list type, only a list of integers is supported, e.g. key=[1, 2] is supported, while not for key=[[1, 2]].
  • Ellipsis (…) and np.newaxis are not supported.
  • Boolean array indexing is not supported.
Parameters:key (int, slice, list, np.ndarray, NDArray, or tuple of all previous types) – Indexing key.

Examples

>>> x = mx.nd.arange(0,6).reshape((2,3))
>>> x.asnumpy()
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.]], dtype=float32)
>>> x[1].asnumpy()
array([ 3.,  4.,  5.], dtype=float32)
>>> y = x[0:1]
>>> y[:] = 2
>>> x.asnumpy()
array([[ 2.,  2.,  2.],
       [ 3.,  4.,  5.]], dtype=float32)
>>> x = mx.nd.arange(0, 8, dtype='int32').reshape((2, 2, 2))
>>> x[[0, 1]]
[[[0 1]
  [2 3]]
 [[4 5]
  [6 7]]]
>>> x[1:, [0, 1]]
[[[4 5]
  [6 7]]]
>>> y = np.array([0, 1], dtype='int32')
>>> x[1:, y]
[[[4 5]
  [6 7]]]
>>> y = mx.nd.array([0, 1], dtype='int32')
>>> x[1:, y]
[[[4 5]
  [6 7]]]