Reshape a Numpy array

In Python Numpy, the number of elements in each dimension of an array is called the shape. To reshape the number of dimensions, use the numpy.reshape() method in Python.

Why reshape?

Reshape allows you to edit the number of elements in a dimension. Additionally, with reshape(), you can add or remove dimensions.

Let us see two examples to reshape arrays in NumPy:

  • Reshape dimensions from 1D to 2D
  • Convert 3D Array to 1D array (Flattening the array)

Reshape dimensions from One-Dimensional to Two-Dimensional

Let us see an example and create a 1D array. We will reshape this 1D array to 2D array using the reshape():

Output

Convert 3D Array to 1D array (Flattening the array)

Let us see an example and create a 3D array. We will reshape this 3D array to 1D array using the reshape():

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now


Read More:

NumPy Array Shape
How to Iterate Numpy Arrays
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading