numpy - why Z[(0,2)] can be view for some cases and be copy in the others?

孤街醉人 提交于 2021-01-29 17:25:32

问题


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)}

回答1:


In [195]: Y = np.arange(12).reshape(3,4)
In [196]: Y
Out[196]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

Using 2 scalar indices on a 2d array returns one element from the array, a numpy scalar.

In [197]: Y[0,1]
Out[197]: 1
In [198]: type(_)
Out[198]: numpy.int64

For most purposes we can view such an object as Python integer. It does have ndarray like attributes such as shape and even __array_interface__, but it is not a ndarray.

In contrast a 'slice' of an array itself an array:

In [199]: Y[0,:]
Out[199]: array([0, 1, 2, 3])
In [200]: type(_)
Out[200]: numpy.ndarray

https://numpy.org/doc/stable/reference/arrays.scalars.html

This docs has a small section showing how an array scalar can be treated as a 0d array.

In [209]: Y.__array_interface__['data']
Out[209]: (38135024, False)
In [210]: Y[0,0].__array_interface__['data']
Out[210]: (35130688, False)
In [211]: Y[0,0,...].__array_interface__['data']
Out[211]: (38135024, False)
In [212]: Y[0,0]             # array scalar
Out[212]: 0
In [213]: Y[0,0,...]         # 0d array
Out[213]: array(0)

To get an element of the array, as a 'plain' Python type:

In [215]: Y.item(0,0)
Out[215]: 0
In [216]: type(_)
Out[216]: int


来源:https://stackoverflow.com/questions/65501307/numpy-why-z0-2-can-be-view-for-some-cases-and-be-copy-in-the-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!