np.reshape - How to Manipulate Arrays Using NumPy's Reshape Function

NumPy is the most popular Python library for numerical and scientific computing.

NumPy's most important capability is the ability to use NumPy arrays, which is its built-in data structure for dealing with ordered data sets.

The np.reshape function is an import function that allows you to give a NumPy array a new shape without changing the data it contains. In this tutorial, I will teach you how to use the NumPy reshape function to manipulate arrays in NumPy.

Table of Contents

How to Import NumPy

To use the np.reshape function, you will first need to import the NumPy programming library.

We typically import NumPy under the alias np. You can do this with the following Python statement:

import numpy as np

Now that NumPy has been imported, you are ready to begin working with the np.reshape function!

Return to the Table of Contents

What is np.reshape?

NumPy's reshape function allows you to transform a NumPy array's shape without changing the data that it contains.

As an example, you can use np.reshape to take a 3x2 NumPy array and transform it into a 6x1 NumPy array.

The np.reshape function takes in three arguments:

  • a - the NumPy array that you want the reshape method to be applied to
  • newshape - the new shape that will be returned from the reshape function
  • order - this is an optional argument that tells the reshape function which order to read the elements of a in.

Let's talk a bit more about the order parameter before learning how to use this important NumPy method.

The order parameter has three possible values - C, F, and A.

A value of C will tell the reshape method to read and write the elements using a C-like index order, with the last axis index changing fastest.

A value of F tells the reshape function to read and write the elements using Fortran-like index order, with the first index changing fastest and the last index changing the slowest.

Lastly, a value of A tells the reshape function to read and write the elements according to the following specifications:

  • Read and write the elements in Fortran-like index order if the NumPy array is Fortran contiguous in memory
  • Read and write the elements in C-like order otherwise

With that out of the way, let's explore a few examples of the np.reshape function in action!

Return to the Table of Contents

Examples of NumPy's np.reshape Function

Throughout this section of this tutorial, we will be using the np.arange function to create sample data sets. If you're unfamiliar with how this method works, you can read this tutorial on np.arange() to learn more.

Example 1

First, let's create a one-dimensional NumPy of length 16 using the np.arange function. We will assign this to a variable called array like this:

array = np.arange(16)

Here's what this data structure looks like if you print it to the console;

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Let's transform this into an 8x2 NumPy array using the following statement:

np.reshape(array, (8,2))

The function will return the following data structure:

array([[ 0,  1],

       [ 2,  3],

       [ 4,  5],

       [ 6,  7],

       [ 8,  9],

       [10, 11],

       [12, 13],

       [14, 15]])

Note that this does not actually change the value assigned to the array variable. To see this, print the array variable to the console using the print(array) statement. It will print the same value as before:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

How can you actually change the value of the array variable using the np.reshape function?

You'll need to overwrite the variable's value using the = assignment operator if you actually want to change the data structure that is assigned to the array variable.

Here is an example:

array = np.reshape(array, (8,2))

Example 2

Let's consider another example of how to use the np.reshape method to change the dimensions of a NumPy array.

Let's again start with a one-dimensional NumPy array of length 16 created using the np.arange method:

second_array = np.arange(16)

Let's transform this new second_array data structure into a 4x4 NumPy array using the following statement:

np.reshape(second_array, (4,4))

This will return the following data structure:

array([[ 0,  1,  2,  3],

       [ 4,  5,  6,  7],

       [ 8,  9, 10, 11],

       [12, 13, 14, 15]])

So far in this tutorial, we have only seen how to transform one-dimensional NumPy arrays into two-dimensional NumPy arrays

It is also possible to do the opposite. In our third example, we will see how to transform a two-dimensional NumPy array into a one-dimensional NumPy array.

Example 3

Let's start by creating a 4x4 NumPy array with the following statement:

third_array = np.reshape(np.arange(16), (4,4))

If you print this third_array variable to the console, you can get a sense of what this 4x4 NumPy array looks like:

array([[ 0,  1,  2,  3],

       [ 4,  5,  6,  7],

       [ 8,  9, 10, 11],

       [12, 13, 14, 15]])

Here is the statement you could use to transform third_array into one-dimensional NumPy array of length 16:

np.reshape(third_array, 16)

This statement returns the following data structure:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

We have only dealt with one-dimensional and two-dimensional NumPy arrays in this tutorial. In our last example, you will learn how to create three-dimensional NumPy arrays using the np.reshape method.

Example 4

Let's start by creating a one-dimensional NumPy array of length 9. We will call the array my_array:

fourth_array = np.arange(9)

Here's what this data structure looks like if you print it:

[0 1 2 3 4 5 6 7 8]

To transform this data structure into a three-dimensional NumPy array with three rows and three columns, you would use the following Python statement:

np.reshape(fourth_array, (3,3))

This returns the following data structure:

array([[0, 1, 2],

       [3, 4, 5],

       [6, 7, 8]])

Return to the Table of Contents

Final Thoughts

In this tutorial, you learned how to use NumPy's np.reshape() method to transform the dimensions of NumPy arrays. Specifically, we discussed:

  • The parameters used by the np.reshape() method
  • How to transform one-dimensional NumPy arrays into two-dimensional NumPy arrays
  • How to create three-dimensional NumPy arrays

If you're interested in learning more about the NumPy programming library, try my Advanced Python for Finance course, which covers NumPy (as well as pandas) in great detail.

If you enjoyed this article, be sure to join my Developer Monthly newsletter, where I send out the latest news from the world of Python and JavaScript:


Written on May 2nd, 2020