How to Reshape a NumPy Array using np.reshape?

This recipe will cover practical examples to reshape a NumPy array using np.reshape function to boost your NumPy skills. | ProjectPro

NumPy is a powerful library in Python for numerical and matrix operations. One of its key features is the ability to reshape arrays, allowing users to modify the structure of their data efficiently. Check out these NumPy code examples to explore the NumPy reshape function and delve into examples of reshaping 1D and 3D arrays into 2D arrays.

Master the Art of Data Cleaning in Machine Learning

What is Reshape in NumPy?

The reshape function in NumPy allows you to give a new shape to an array without changing its data. It returns a new array with the same data but a different shape. This functionality is particularly useful when working with different dimensions of data, like transforming a 1D array into a 2D array or reshaping a 3D array into a 2D array.

How to Reshape a NumPy Array using np.reshape?

To use the reshape function, you need to call it on a NumPy array and provide the desired new shape as an argument. The shape is specified as a tuple of integers representing the dimensions. It's essential to ensure that the total number of elements in the original array matches the total number of elements in the reshaped array.

Check below the general syntax - 

NumPy reshape function syntax

Now, let's check out the specific examples of reshaping 1D and 3D arrays into 2D arrays.

NumPy Reshape 1D to 2D Array - Example

Consider the following 1D NumPy array: 

1D NumPy array

If you want to reshape this 1D array into a 2D array with 2 rows and 5 columns, you can use the reshape function. 

The resulting 2D array would look like:

[[1 2 3 4 5]

 [6 7 8 9 10]]

Numpy reshape 1d to 2d

There is another method to reshape the array directly using reshape function - 

We have a 1D array with 6 elements, and we want to reshape it into a 2D array with 2 rows and 3 columns using the reshape()method. Check it below - 

Example -Numpy reshape 1d to 2d

NumPy Reshape 3D to 2D Array - Example

Now, let's consider a 3D NumPy array:

NumPy 3D array

If you want to reshape this 3D array into a 2D array with 3 rows and 4 columns, the resulting 2D array would look like:- 

[[ 1  2  3  4]

 [ 5  6  7  8]

 [ 9 10 11 12]]

NumPy reshape 3D to 2D array

Another method to reshape a NumPy array with 3 rows and 4 columns using reshape method - 

Example - NumPy reshape 3D to 2D array

Reshape a 4 x 3 Matrix in Different Ways - Example 

Step 1 - Import the library

    import numpy as np

We have only imported numpy which is needed.

Step 2 - Setting up the Vector and Matrix

We have created a 4 x 3 matrix using array and we will reshape it.

    matrix = np.array([[11, 22, 33],

                       [44, 55, 66],

                       [77, 88, 99],

                       [110, 121, 132]])

Step 3 - Reshaping a matrix

We can reshape the matrix by using the reshape function. In the function we have to pass the shape of the final matrix we want. (If we want a matrix of n by m then we have to pass (n,m)).

    print(matrix.reshape(2, 6))

    print(matrix.reshape(3, 4))

    print(matrix.reshape(6, 2))

So the output comes as

[[ 11  22  33  44  55  66]

 [ 77  88  99 110 121 132]]

[[ 11  22  33  44]

 [ 55  66  77  88]

 [ 99 110 121 132]]

[[ 11  22]

 [ 33  44]

 [ 55  66]

 [ 77  88]

 [ 99 110]

 [121 132]]

Practice more NumPy Operations with ProjectPro! 

Proficiency in NumPy operations, including reshaping arrays with np.reshape, is paramount for effective data handling in Python. The key to mastering these skills lies in practical application through real-world projects. With ProjectPro's extensive collection of over 270+ projects in data science and big data, aspiring learners can immerse themselves in hands-on experiences, honing their abilities and fostering a deeper understanding of NumPy and its applications. Subscribe to ProjectPro Repository to bridge the gap between theoretical knowledge and practical expertise, paving the way for success in the dynamic field of data analysis.


Download Materials


What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build an AI Quiz Generator from Video with OpenAI API
In this LLM project, you will build a model to automate the transcription of video content and generate interactive quizzes using OpenAI’s Whisper and GPT-4o.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.