Behavior of the slicing different from numpy


#1
a = mx.nd.array(np.arange(9).reshape(3,3))
b = a[0:1,0:1]
c = a[:]
a[0:1,0:1] = 10
print(a)
print(b)
print(c)

The above result it

[[10.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]]
<NDArray 3x3 @cpu(0)>

[[0.]]
<NDArray 1x1 @cpu(0)>

[[10.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]]
<NDArray 3x3 @cpu(0)>

If a were a numpy array, the second result was [[10.]] rather than [[0.]]. Is this a bug or a feature? I couldn’t find any documentation on this behavior.

Another question is whether slice and slice_axis methods have any advantage over the slicing syntax.

Thank you.


#2

This is because the two numpy arrays a and b refer to the same data, while the two ndarrays a and b don’t.


#3

There is only one case under which ndarray indexing doesn’t create a copy and that’s if you call a[n:m] with n and m integers. This GitHub issue contains details of this behavior.