Table Of Contents
Table Of Contents

NDArray.__setitem__

NDArray.__setitem__(key, value)[source]

x.__setitem__(i, y) <=> x[i]=y

Sets value to self[key]. 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) – The indexing key.
  • value (scalar or array-like object that can be broadcast to the shape of self[key]) – The value to set.

Examples

>>> x = mx.nd.zeros((2,3))
>>> x[:] = 1
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> x.asnumpy()
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> x[:,1:2] = 2
>>> x.asnumpy()
array([[ 1.,  2.,  1.],
       [ 1.,  2.,  1.]], dtype=float32)
>>> x[1:2,1:] = 3
>>> x.asnumpy()
array([[ 1.,  2.,  1.],
       [ 1.,  3.,  3.]], dtype=float32)
>>> x[1:,0:2] = mx.nd.zeros((1,2))
>>> x.asnumpy()
array([[ 1.,  2.,  1.],
       [ 0.,  0.,  3.]], dtype=float32)
>>> x[1,2] = 4
>>> x.asnumpy()
array([[ 1.,  2.,  1.],
       [ 0.,  0.,  4.]], dtype=float32)
>>> x[[0], [1, 2]] = 5
>>> x.asnumpy()
array([[ 1.,  5.,  5.],
       [ 0.,  0.,  4.]], dtype=float32)
>>> x[::-1, 0:2:2] = [6]
>>> x.asnumpy()
array([[ 6.,  5.,  5.],
       [ 6.,  0.,  4.]], dtype=float32)