1
votes

On the numpy indexing page, there is a warning paragraph

The definition of advanced indexing means that x[(1,2,3),] is fundamentally different than x[(1,2,3)]. The latter is equivalent to x[1,2,3] which will trigger basic selection while the former will trigger advanced indexing. Be sure to understand why this occurs.

I tried to run the following code

import numpy as np

x = np.arange(3*4).reshape((3, 4))
y = x[(1, 2)]
z = x[(1, 2),]

print("base:", x.base, y.base, z.base)
print("id:", id(x.base), id(y.base), id(z.base))
print(np.shares_memory(x, y), np.shares_memory(x, z))

and got the results as

base: [ 0  1  2  3  4  5  6  7  8  9 10 11] None None
id: 4299634928 4297628200 4297628200
False False

It seems that y doesn't return a view and thus x[(1, 2)] can't be a basic indexing because

All arrays generated by basic slicing are always views of the original array.

Is it a mistake in the documentation? Or did I misunderstand somewhere?

1
Better test / validate this with x.flags and if OWNDATA: False , such an object still remains ( since it's original mode of instantiation ) a view onto some zone of the underlying numpy array. - user3666197

1 Answers

2
votes

y isn't a view because it's a scalar, not an array. All arrays generated by basic slicing are always views of the original array.