python - Is OWNDATA a false negative in this case? - Stack Overflow

I've been testing some operations in numpy to get a better grasp of what copy and view are. Then I

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.

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.

Share Improve this question edited Mar 21 at 20:13 BigBen 50.2k7 gold badges28 silver badges44 bronze badges asked Mar 21 at 19:50 excitedGooseexcitedGoose 2111 silver badge3 bronze badges 1
  • foo=arr[:,[0,1]] is another case where the base is the actual copy, foo is a transpose of that base. – hpaulj Commented Mar 22 at 0:31
Add a comment  | 

1 Answer 1

Reset to default 2

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744336032a4569120.html

相关推荐

  • python - Is OWNDATA a false negative in this case? - Stack Overflow

    I've been testing some operations in numpy to get a better grasp of what copy and view are. Then I

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信