Numpy, the array doesn't have its own data?

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

I tried to use resize on an array in this way:

a = np.array([1,2,3,4,5,6], dtype=np.uint8) a.resize(4,2) print a  

and the output is Ok!(I meant that there was no error). But when I run this code:

a = np.array([1,2,3,4,5,6], dtype=np.uint8).reshape(2,3) a.resize(4,2) print a  

it gave rise to an error, saying that, ValueError: cannot resize this array: it does not own its data

My question: why after applying reshape the ownership of array is changed? The ownership is granted to whom !? The reshape does not create a new memory and it is performing its operation on the same array memory! So why the ownership will change?

I read np.reshape and ndarray.resize doc but I can not understand the reason. I read this post. I can check ndarray.flags always before applying the resize method.

回答1:

Lets start with the following:

>>> a = np.array([1,2,3,4,5,6], dtype=np.uint8) >>> b = a.reshape(2,3) >>> b[0,0] = 5 >>> a array([5, 2, 3, 4, 5, 6], dtype=uint8) 

I can see here that array b is not its own array, but simply a view of a (just another way to understand the "OWNDATA" flag). To put it simply both a and b reference the same data in memory, but b is viewing a with a different shape. Calling the resize function like ndarray.resize tries to change the array in place, as b is just a view of a this is not permissible as from the resize definition:

The purpose of the reference count check is to make sure you do not use this array as a buffer for another Python object and then reallocate the memory.


To circumvent your issue you can call resize from numpy (not as an attribute of a ndarray) which will detect this issue and copy the data automatically:

>>> np.resize(b,(4,2)) array([[5, 2],        [3, 4],        [5, 6],        [5, 2]], dtype=uint8) 

Edit: As CT Zhu correctly mention np.resize and ndarray.resize add data in two different ways. To reproduce expected behavior as ndarray.resize you would have to do the following:

>>> c = b.copy() >>> c.resize(4,2) >>> c array([[5, 2],        [3, 4],        [5, 6],        [0, 0]], dtype=uint8) 


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