Advanced indexing with selection object

Hi!

I’ve NDArray tensor after the operations:

with open(lbl_path, ‘rb’) as fp:
str_lbl = fp.read()
lbl = mx.img.imdecode(str_lbl, 0)
lbl = mx.ndarray.reshape(lbl, (lbl.shape[0], lbl.shape[1]))

shape of lbl (1000 x 500)

After that I try replace elements by selected indexes:

lbl[lbl == 1] = 100

I got error:
[bt] (0) /home/user/anaconda3/envs/mx/lib/python3.5/site-packages/mxnet/libmxnet.so(+0x34b75a) [0x7f0eb77d675a]
[bt] (9) /home/user/anaconda3/envs/mx/lib/python3.5/site-packages/mxnet/libmxnet.so(+0x28ae574) [0x7f0eb9d39574]
terminate called after throwing an instance of ‘std::system_error’
what(): Invalid argument

But this example is working fine:

ones = mx.nd.ones((100, 100))
ones[ones==1] = 5

Hi @Pavel_Kurnikov,

lbl[lbl == 1] isn’t actually all the values of lbl that are equal to 1, so that’s why the assignment doesn’t work. So even after calling ones[ones==1] = 5, if you check ones it’s not actually an array of 5s, but instead:

[[1. 1. 1. ... 1. 1. 1.]
 [5. 5. 5. ... 5. 5. 5.]
 [1. 1. 1. ... 1. 1. 1.]
 ...
 [1. 1. 1. ... 1. 1. 1.]
 [1. 1. 1. ... 1. 1. 1.]
 [1. 1. 1. ... 1. 1. 1.]]
<NDArray 100x100 @cpu(0)>

You’ll want to use mx.nd.where. Something like the following should work:

data = mx.nd.random.uniform(low=0, high=3, shape=(3,3)).round()

# [[1. 2. 0.]
#  [1. 0. 2.]
#  [2. 1. 0.]]
# <NDArray 3x3 @cpu(0)>

mx.nd.where(condition=(data == 0),
            x=mx.nd.ones_like(data)*100,
            y=data)

# [[  1.   2. 100.]
#  [  1. 100.   2.]
#  [  2.   1. 100.]]
# <NDArray 3x3 @cpu(0)>