pythonnumpy

Is OWNDATA a false negative in this case?


I've been testing some operations in numpy to get a better grasp of what copy and view are. Then I ran into the following code:

import numpy as np

arr = np.array([[1,2,7],[3,4,9],[5,6,0]])  

sliced_arr = arr[:,0:2] 

reshaped = sliced_arr.reshape(6,)  

print(reshaped.flags)
print(reshaped.base is arr)
reshaped[0] = 100  

print(reshaped) 
print(arr)

Output:

C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False

False
[100   2   3   4   5   6]
[[1 2 7]
 [3 4 9]
 [5 6 0]]

Which shows that reshaped behaves like a copy given it doesn't modify arr. How is it possible reshaped not to be a view of arr, if sliced_arr is a view of arr? Is that OWNDATA a false negative here? I would like to know what is going on under the hood in this case.


Solution

  • reshaped is a view of a copy of sliced_arr. It does not own its data.

    When the NumPy docs talk about whether something will be a view or a copy, this only refers to whether it will be a view of the original input. Something documented as returning a view will make a view of the original input. Something documented as returning a copy will return an array backed by a new data buffer, but the array might not be the owner of that buffer. So when the reshape docs say

    This will be a new view object if possible; otherwise, it will be a copy.

    that doesn't mean that the "copy" case will own its data.