Numpy Reshape Function

NumPyNumPyBeginner
Practice Now

Introduction

The reshape() function in the NumPy library is mainly used to change the shape of an array without changing its underlying data. It helps in providing a new shape to an array that can be useful based on your use case. In this lab, we will cover the basic usage of the reshape() function and the different parameters and values it uses.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) numpy(("`NumPy`")) -.-> numpy/ArrayManipulationGroup(["`Array Manipulation`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") numpy/ArrayManipulationGroup -.-> numpy/reshape("`Reshape`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-86496{{"`Numpy Reshape Function`"}} python/with_statement -.-> lab-86496{{"`Numpy Reshape Function`"}} numpy/reshape -.-> lab-86496{{"`Numpy Reshape Function`"}} python/tuples -.-> lab-86496{{"`Numpy Reshape Function`"}} python/importing_modules -.-> lab-86496{{"`Numpy Reshape Function`"}} python/numerical_computing -.-> lab-86496{{"`Numpy Reshape Function`"}} python/build_in_functions -.-> lab-86496{{"`Numpy Reshape Function`"}} end

Importing NumPy Library

In the first step, we will import the NumPy library and assign it an alias np. This will allow us to use its properties and functions in our code:

import numpy as np

Reshaping the array

In this step, we will create an array and reshape it using the reshape() function. The reshape() function takes two parameters: the first parameter is the existing array, and the second parameter is the new shape we want to give to the array.

a = np.arange(12)
print("The Original array : \n", a)

## reshaping the array to have 2 rows and 6 columns
a1 = np.reshape(a, (2, 6))
print("\nThe reshaped array with 2 rows and 6 columns : \n", a1)

## reshaping the array to have 6 rows and 2 columns
a2 = np.reshape(a, (6, 2))
print("\nThe reshaped array with 6 rows and 2 columns : \n", a2)

Creating a 3D array

In this step, we will create a 3D array using reshape() function with the new shape provided as (2, 3, 2) as mentioned below:

a3 = np.reshape(a, (2, 3, 2))
print("\nAfter reshaping the original array to 3D : \n", a3)

Fortran-like Index Ordering

In this step, we will learn how to use reshape() function for Fortran-like index ordering. The order parameter is used to control the order of the elements in the array. Fortran-like index ordering is when the last axis index is changing slowest, and the first axis index is changing fastest.

x = np.arange(12)
print("The array is :\n",x)

## reshaping the array using Fortran-like index ordering
y = np.reshape(x, (4, 3), order='F')
print("Reshaping the original array using Fortran-like index ordering\n", y)

C-like Index Ordering

In this step, we will learn how to use reshape() function for C-like index ordering.

x = np.arange(12)
print("The array is :\n",x)

## reshaping the array using C-like index ordering
y = np.reshape(x, (4, 3), order='C')
print("Reshaping the original array using C-like index ordering\n", y)

Summary

In this lab, we learned about the reshape() function of the NumPy library. We covered its syntax, parameters, values returned by this function and how to use it with code samples. With the reshape() function, we are able to change the shape of an array as per our requirements, without affecting the original data of the array.

Other NumPy Tutorials you may like