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

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python