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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.