Reputation: 361
I have just started using NumPy. What is the difference between resize
and reshape
for arrays?
Upvotes: 31
Views: 35442
Reputation: 14141
reshape()
is able to change the shape only (i.e. the meta info), not the number of elements.
If the array has five elements, we may use e.g. reshape(5, )
, reshape(1, 5)
,
reshape(1, 5, 1)
, but not reshape(2, 3)
.
reshape()
in general don't modify data themselves, only meta info about them,
the .reshape()
method (of ndarray) returns the reshaped array, keeping the original array untouched.
resize()
is able to change both the shape and the number of elements, too.
So for an array with five elements we may use resize(5, 1)
, but also resize(2, 2)
or resize(7, 9)
.
.resize()
method (of ndarray) returns None
, changing only the original array (so it seems as an in-place change).Upvotes: 8
Reputation: 146
One more point is:
np.reshape can take -1 in one dimension. np.resize can't.
Example as below:
arr = np.arange(20)
arr.resize(5, 2, 2)
arr.reshape(2, 2, -1)
Upvotes: 0
Reputation: 9
Suppose you have the following np.ndarray:
a = np.array([1, 2, 3, 4]) # Shape of this is (4,)
Now we try 'a.reshape'
a.reshape(1, 4)
array([[1, 2, 3, 4]])
a.shape # This will again return (4,)
We see that the shape of a
hasn't changed.
Let's try 'a.resize' now:
a.resize(1,4)
a.shape # Now the shape changes to (1,4)
'resize' changed the shape of our original NumPy array a (It changes shape 'IN-PLACE').
Upvotes: 0
Reputation: 1030
One major difference is reshape() does not change your data, but resize() does change it. resize() first accommodates all the values in the original array. After that, if extra space is there (or size of new array is greater than original array), it adds its own values. As @David mentioned in comments, what values resize() adds depends on how that is called.
You can call reshape()
and resize()
function in the following two ways.
numpy.resize()
ndarray.resize()
- where ndarray
is an n
dimensional array you are resizing.
You can similarly call reshape also as numpy.reshape()
and ndarray.reshape()
. But here they are almost the same except the syntax.
One point to notice is that, reshape() will always try to return a view wherever possible, otherwise it would return a copy. Also, it can't tell what will be returned when, but you can make your code to raise error whenever the data is copied.
For resize() function, numpy.resize()
returns a new copy of the array whereas ndarray.resize()
does it in-place. But they don't go to the view
thing.
Now coming to the point that what the values of extra elements should be. From the documentation, it says
If the new array is larger than the original array, then the new array is filled with repeated copies of a. Note that this behavior is different from a.resize(new_shape) which fills with zeros instead of repeated copies of a.
So for ndarray.resize()
it is the value 0
, but for numpy.resize()
it is the values of the array itself (of course, whatever can fit in the new size). The below code snippet will make it clear.
In [40]: arr = np.array([1, 2, 3, 4])
In [41]: np.resize(arr, (2,5))
Out[41]:
array([[1, 2, 3, 4, 1],
[2, 3, 4, 1, 2]])
In [42]: arr.resize((2,5))
In [43]: arr
Out[43]:
array([[1, 2, 3, 4, 0],
[0, 0, 0, 0, 0]])
You can also see that ndarray.resize()
returns None
and does the resizing in-place.
Upvotes: 10
Reputation: 1155
Reshape doesn't change the data as mentioned here. Resize changes the data as can be seen here.
Here are some examples:
>>> numpy.random.rand(2,3)
array([[ 0.6832785 , 0.23452056, 0.25131171],
[ 0.81549186, 0.64789272, 0.48778127]])
>>> ar = numpy.random.rand(2,3)
>>> ar.reshape(1,6)
array([[ 0.43968751, 0.95057451, 0.54744355, 0.33887095, 0.95809916,
0.88722904]])
>>> ar
array([[ 0.43968751, 0.95057451, 0.54744355],
[ 0.33887095, 0.95809916, 0.88722904]])
After reshape the array didn't change, but only output a temporary array reshape.
>>> ar.resize(1,6)
>>> ar
array([[ 0.43968751, 0.95057451, 0.54744355, 0.33887095, 0.95809916,
0.88722904]])
After resize the array changed its shape.
Upvotes: 27