In this tutorial, you’ll learn how to use NumPy reshape() to reshape NumPy arrays without changing the original data.
When working with Numpy arrays, you may often want to reshape an existing array into an array of different dimensions. This can be particularly useful when you transform data in multiple steps.
And NumPy reshape() helps you do it easily. Over the next few minutes, you’ll learn the syntax to use reshape(), and also reshape arrays to different dimensions.
What is Reshaping in NumPy Arrays?
When working with NumPy arrays, you may first want to create a 1-dimensional array of numbers. And then reshape it to an array with the desired dimension.
This is particularly helpful when the dimensions of the new array are not known initially or are inferred during execution. Or it may also be possible that a certain data processing step requires the input to be of a specific shape.
Here’s where reshaping comes in handy.
For example, consider the following illustration. We have a vector—a one-dimensional array of 6 elements. And we can reshape it into arrays of shapes 2×3, 3×2, 6×1, and so on.
▶️ To follow along with the examples in this tutorial, you need to have Python and NumPy installed. If you don’t have NumPy yet, check out our NumPy installation guide.
You may now go ahead and import NumPy under the alias np, by running: import numpy as np.
Let’s proceed to learn the syntax in the next section.
Syntax of NumPy reshape()
Here’s the syntax to use NumPy reshape():
np.reshape(arr, newshape, order = 'C'|'F'|'A')
arr is any valid NumPy array object. Here, it’s the array to be reshaped.
newshape is the shape of the new array. It can be either an integer or a tuple.
When newshape is an integer, the returned array is one-dimensional.
order refers to the order in which you’d like to read in the elements of the array to be reshaped.
The default value is ‘C’, which means the elements of the original array will be read in a C-like indexing order (starting with 0)
‘F’ stands for Fortran-like indexing (starting with 1). And ‘A’ reads in the elements in either C-like or Fortran-like order depending on the memory layout of the array arr.
So what does np.reshape() return?
It returns a reshaped view of the original array if possible. Else, it returns a copy of the array.
In the above line, we mentioned that NumPy reshape() would try to return a view whenever possible. Else, it returns a copy. Let’s proceed to discuss the differences between a view and a copy.
View vs. Copy of NumPy Arrays
As the name suggests, copy is a copy of the original array. And any changes made to the copy will not affect the original array.
On the other hand, view simply refers to reshaped view of the original array. This means that any change made to the view will also affect the original array and vice versa.
Use NumPy reshape() to Reshape 1D Array to 2D Arrays
#1. Let’s start by creating the sample array using np.arange().
We need an array of 12 numbers, from 1 to 12, called arr1. As the NumPy arange() function excludes the endpoint by default, set the stop value to 13.
Now let us use the above syntax, and reshape arr1 with 12 elements into a 2D array of shape (4,3). Let’s call this arr2 with 4 rows, and 3 columns.