Continuation to the question numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?. I got the answer and understood that comma triggering advanced index makes a totally different indexing. The answer also provided a way using __array_interface__
to understand copy/view behavior.
However apparently I have not got to the bottom of this part of the answer.
A view is returned if the new array can be described with shape, strides and all or part of the original data buffer
Because I still cannot explain why the behavior below are different. Hence opening a new question to get to the bottom of this (shape, stride)
mechanism to understand how view/copy works.
Kindly elaborate how numpy uses shhape
and stride
to determine to return a copy or not.
The first indexingX[0,1]
returns a view.
X = np.arange(36).reshape(3, 3, 4)
print("X is \n{}\n".format(X))
print("X.__array_interface__ \n{}\n".format(X.__array_interface__))
_x = X[
0,
2
]
print("X[0,1] is \n{}\nIs view? {}\n".format(
_x,
_x.base is not None
))
print("_x.__array_interface__ is \n{}\n".format(_x.__array_interface__))
---
X is
[[[ 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 27]
[28 29 30 31]
[32 33 34 35]]]
X.__array_interface__
{'data': (94194168544480, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (3, 3, 4), 'version': 3}
X[0,1] is
[ 8 9 10 11]
Is view? True
_x.__array_interface__ is
{'data': (94194168544544, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (4,), 'version': 3}
However, the next indexing with the same [0,1]
returns a copy.
Y = np.arange(12).reshape(3, 4)
print("Y is \n{}\n".format(Y))
print("Y.__array_interface__ is \n{}\n".format(Y.__array_interface__))
_y = Y[
0,
1
]
print("Y[0,1] is \n{}\nIs view {}\n".format(
_y,
_y.base is not None
))
print(".Y[0,1].__array_interface__ is \n{}\n".format(_y.__array_interface__))
---
Y is
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Y.__array_interface__ is
{'data': (94194175499568, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (3, 4), 'version': 3}
Y[0,1] is
1
Is view False
.Y[0,1].__array_interface__ is
{'data': (94194177613184, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (), 'version': 3, '__ref': array(1)}