Change shape and size of array in-place.
new_shape : tuple of ints, or n ints
refcheck : bool, optional
order : bool, do not use.
This reallocates space for the data area if necessary.
Only contiguous arrays (data elements consecutive in memory) can be resized.
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. However, reference counts can increase in other ways so if you are sure that you have not shared the memory for this array with another Python object, then you may safely set refcheck to False.
Shrinking an array: array is flattened (in the order that the data are stored in memory), resized, and reshaped:
>>> a = np.array([[0, 1], [2, 3]]) >>> print a.flags C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False >>> a.resize((2, 1)) >>> a array([, ])
>>> a = np.array([[0, 1], [2, 3]], order='F') >>> print a.flags C_CONTIGUOUS : False F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCOPY : False >>> a.resize((2, 1)) >>> a array([, ])
Enlarging an array: as above, but missing entries are filled with zeros:
>>> b = np.array([[0, 1], [2, 3]]) >>> b.resize(2, 3) # new_shape parameter doesn't have to be a tuple >>> b array([[0, 1, 2], [3, 0, 0]])
Referencing an array prevents resizing...
>>> c = a >>> a.resize((1, 1)) ... ValueError: cannot resize an array that has been referenced ...
Unless refcheck is False:
>>> a.resize((1, 1), refcheck=False) >>> a array([]) >>> c array([])