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

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

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.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Autogen Project to Build an Intelligent AI Personal Assistant
Build a multi-agent AI personal assistant using Autogen that can handle tasks like managing calendars, emails, reminders, messaging, research, and weather updates, automating everyday workflows with LLMs and tool integrations. This is an upcoming project that is expected to be launched in June.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT