Difference between reshape() and resize() method in Numpy


The size of the NumPy arrays can be changed with the help of two functions in python namely reshape and resize. There's only one thing that differentiates them, the original array remains unchanged while using resize() while reshape() returns only the changed array and the original array remains intact.

Syntax

Reshape()

reshape(x,y)

In this syntax, x specifies the number of smaller arrays that has to be created from the larger array provided as input and y denotes the actual number of elements present in the array.

The method returns a modified array if the values are practical and returns error if the array could not be divided into the specified number of smaller arrays.

Resize()

resize(x,y)

The function is very similar to the reshape() function when determining the x and y parameters. The return pattern also remains same.

In addition, the original array is changed permanently.

Example 1

This explains the usage of reshape() attribute to manipulate the features of the array. In this example, an array of 8 elements is reshaped in such a way that it gets divided into 4 smaller arrays each containing 2 elements.

Algorithm

Step 1: import numpy as np

Step 2: create a numpy array from the given array.

Step 3: use the reshape function by passing the number of elements in each smaller array and number of such smaller arrays.

Step 4: print the array.

Example

#import numpy
import numpy as np
 
#create a numpy array with values of the array.
data = np.array([10, 20, 30, 40, 50, 60, 70, 80])

#print the contents of the original numpy array
print("Original array: ")
display(data)

#reshape the array into a 4x2 matrix and display it.
print("Reshaped array: ")
display(data.reshape(4, 2))

#print the original array 
print("Original array:")
display(data)

Output

Original array: 
array([10, 20, 30, 40, 50, 60, 70, 80])

Reshaped array: 
array([[10, 20],
    [30, 40],
    [50, 60], 
    [70, 80]])

From the output, we can see that the original array remains unchanged even after the usage of reshape() . The changes are made in the buffer array which has the dimensions required.

Example 2

This code explains the usage of resize() attribute to manipulate the features of the array. Here , an array of 8 elements is resized in such a way that it gets divided into 4 arrays each containing 2 elements. Note that the changes are made in the original array itself.

Algorithm

Step 1: import numpy as np

Step 2: create a numpy array from the given array.

Step 3: use the resize function by passing the number of elements in each smaller array and number of such smaller arrays.

Step 4: print the array.

Example

#import numpy
import numpy as np

#create a numpy array with values of the array
data = np.array([10, 20, 30, 40, 50, 60, 70, 80])

#print the contents of the original array
print("Original array: ")
display(data)

#resize the array into a 4x2 matrix
print("Resized array: ")
display(data.resize(4, 2))
 
#print the original array
print("Original array:")
display(data)

Output

Original array: 
array([10, 20, 30, 40, 50, 60, 70, 80])

Resized array: 
None

Original array:
array([[10, 20],
    [30, 40],
    [50, 60],
    [70, 80]])

We can observe that there is no resized array available separately and the original array itself is manipulated to match the dimensions mentioned by the user as parameters in the resize function.

Key differences

Attribute Reshape Resize
Functionality The reshape attribute throws a ValueError if the array is not divisible into the specified number of sub arrays. Value error is observed whenever the array lacks enough elements. In the case of reshape attribute, it adds 0s to fill the array such that it is exactly divisible into the number of fragments mentioned by the user.
In-place switching In the case of reshape() a copy of the existing array is created and the change in dimensions are made to the copy of the array. Thus there is no change in the dimensions of the original array The resize() method uses in-place modifications and so the original array gets overwritten.

Conclusion

Both the methods, reshape() or resize() can be used in a situation where there's no need to preserve the original array. But, if the parameters passed to the functions do not match with the actual size of the array or if the array lacks that many elements then errors are raised.

Reshape is preferred over resize if the original array has to remain intact.

Updated on: 10-Aug-2023

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements