Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python numpy.reshape() function

Python Numpy.Reshape() Function

Hey, folks! Hope you all are doing well. In this article, we will be understanding the working of Python numpy.reshape() function.

As we all know, Python NumPy module provides us with huge amount of functions to manipulate and deal with mathematical data. Here, we will be unveiling the functioning of Numpy reshape() function.

So, let us get started!


Working of Python numpy.reshape() function

Python numpy.reshape() function enables us to reshape an array i.e. change the dimensions of the array elements. Reshaping an array would help us change the number of data values that reside in a particular dimension.

An important point to note is that the reshape() function retains the size of the array i.e. it makes no change in the number of array elements.

Let us now understand the structure of numpy.reshape() function in the upcoming section.


Syntax of Python numpy.reshape() function

array-name.reshape(shape)
  • shape: It is the tuple of integer values, according to which the elements are reshaped.

Let us consider an example to understand the process to pass the shape to the reshape() function.

If we have 16 elements in the input array, so we need to pass such integer values as tuple to the shape parameter that the product of those tuple values equates to the number of elements i.e. 16.

The shape parameter can have the following possibilities:

  • [2,8]
  • [8,2]
  • [4,4]
  • [16,1]
  • [1,16]
  • [4,2,2]

Let us now go through the below examples to have a better understanding about numpy.reshape() function.


Implementing Python numpy.reshape() with examples

In the below example, we have created an 1-D array of 16 elements using numpy.arange() function.

Further, we have reshaped the dimensions of the array into a 2-D array of 4 elements per dimension using reshape() function.

import numpy as np 

arr = np.arange(16) 
print("Array elements: \n", arr) 

res = np.arange(16).reshape(4, 4) 
print("\nArray reshaped as 4 rows and 4 columns: \n", res) 

Output:

Array elements: 
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Array reshaped as 4 rows and 4 columns: 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Now, we have reshaped and converted the 1-D array into array with 2 elements per dimension.

import numpy as np 

arr = np.arange(16) 
print("Array elements: \n", arr) 

res = np.arange(16).reshape(4,2,2) 
print("\nArray reshaped: \n", res) 

Output:

Array elements: 
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Array reshaped: 
 [[[ 0  1]
  [ 2  3]]

 [[ 4  5]
  [ 6  7]]

 [[ 8  9]
  [10 11]]

 [[12 13]
  [14 15]]]

In the below example, we have converted the 2-D array into an array of single dimension by passing value as -1 to the reshape() function.

import numpy as np 

arr = np.array([[1, 2, 3,4], [10, 11, 12,13],[5,6,7,8]])
print("Array elements: \n", arr) 

res = arr.reshape(-1) 
print("\nArray reshaped as 1-D Array: \n", res) 

Output:

Array elements: 
 [[ 1  2  3  4]
 [10 11 12 13]
 [ 5  6  7  8]]

Array reshaped as 1-D Array: 
 [ 1  2  3  4 10 11 12 13  5  6  7  8]

Conclusion

By this, we have come to the end of this topic. Feel free to comment below in case, you come across any doubt. Happy Learning!


References