30

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.

Community
  • 1
  • 1
Hadi
  • 5,328
  • 11
  • 46
  • 67

1 Answers1

29

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)
Daniel
  • 19,179
  • 7
  • 60
  • 74
  • `a = np.array([1,2,3,4,5,6], dtype=np.uint8)`, these two gives different results: `np.resize(a, (4,2))` `a.resize(4,2);print a`. It is not a circumvent – CT Zhu Apr 23 '14 at 19:41
  • @CTZhu Good point they increase the shape in two different ways. – Daniel Apr 23 '14 at 19:46
  • 4
    Another way is to modify the `shape` attribute of the array. (e.g. `a.shape = (2, 3)`) This reshapes it in-place without creating a new view. – Joe Kington Apr 23 '14 at 19:52