Each Answer to this Q is separated by one/two green lines.
I have just started using NumPy. What is the difference between
reshape for arrays?
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.
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
resize() function in the following two ways.
ndarray.resize() – where
ndarray is an
n dimensional array you are resizing.
You can similarly call reshape also as
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
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.
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 : arr = np.array([1, 2, 3, 4]) In : np.resize(arr, (2,5)) Out: array([[1, 2, 3, 4, 1], [2, 3, 4, 1, 2]]) In : arr.resize((2,5)) In : arr Out: array([[1, 2, 3, 4, 0], [0, 0, 0, 0, 0]])
You can also see that
None and does the resizing in-place.
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(1, 5, 1), but not
reshape()in general don’t modify data themselves, only meta info about them,
.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()method (of ndarray) returns
None, changing only the original array (so it seems as an in-place change).
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’).
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)