Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python numpy.reshape() function

  • Safa MulaniSafa Mulani
  • June 30, 2020
  • NumPy
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

  • NumPy reshape() function — Documentation
Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
How to Work with Python TimeDelta?
Previous Post How to Work with Python TimeDelta?
Next Post How to append an Array in Python?
How to append an Array in Python?

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
np.outer(): Outer Product in NumPy

np.outer(): Outer Product in NumPy

  • November 14, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.