In NumPy documentation for advanced indexing, it is mentioned that
Also recognize that
x[[1, 2, 3]]
will trigger advanced indexing, whereasx[[1, 2, slice(None)]]
will trigger basic slicing.
A matrix is stored sequentially into the memory. I understand that it makes sense to make a view of x[[1, 2, slice(None)]]
since the elements are stored sequentially into the memory. But why Numpy returns a view of x[[1, slice(None), 2]]
or x[[slice(None), 1, 2]]
. For instance, assume
x = [[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]]
x[[1, slice(None), 2]]
returns a view of [11, 14, 17]
which is not sequentially stored in the memory as well as for x[[slice(None), 1, 2]]
which returns [5, 14, 23]
.
I would like to know
Why NumPy even returns a view in these two cases
How NumPy handles memory addressing to create these views