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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Data Analysis of Working Capital Management using Tableau
In this Data Analysis Project using Tableau, you will focus on optimizing working capital by analyzing receivables and payables data using Tableau and build actionable dashboards.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Build an AI Insurance Agent for Eligibility Analysis Using CrewAI
Build an AI Insurance Agent that automates eligibility checks by extracting medical details, mapping conditions to policy terms, and generating explainable coverage decisions using CrewAI and LLMs. This is an upcoming project that is expected to be launched in June.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.