In this article we will discuss how to use numpy.reshape() to change the shape of a numpy array.

numpy.reshape()

Python’s numpy module provides a function reshape() to change the shape of an array,

Parameters:

  • a: Array to be reshaped, it can be a numpy array of any shape or a list or list of lists.
  • newshape: New shape either be a tuple or an int.
      • For converting to shape of 2D or 3D array need to pass tuple
      • For creating an array of shape 1D, an integer needs to be passed.
  • order: The order in which items from input array will be used,
      • ‘C’: Read items from array row wise i.e. using C-like index order.
      • ‘F’: Read items from array column wise i.e. using Fortran-like index order.
      • ‘A’: Read items from array based on memory order of items

It returns a new view object if possible, otherwise returns a copy. But in most scenario it returns a view and therefore it is very good in performance with big arrays.

Let’s understand this with more examples,

First, import the numpy module,

Converting shapes of Numpy arrays using numpy.reshape()

Use numpy.reshape() to convert a 1D numpy array to a 2D Numpy array

Let’s first create a 1D numpy array from a list,

Now suppose we want to convert this 1D array to a 2D numpy array or matrix of shape (3X3) i.e. 3 rows and 3 columns. Let’s see how to do that using reshape(),

Output:

We passed the array and a tuple (shape) as arguments to the numpy.reshape() function and it returned a new 2D view of the passed array.

New shape must be compatible with the original shape

The new shape provided in reshape() function must be compatible with the shape of the array passed.
Suppose if we are trying to convert a 1D array of length N to a 2D Numpy array of shape (R,C), then R * C must be equal to N, otherwise it will raise an error. For example,

  • We can convert a numpy array of 9 elements to a 3X3 matrix or 2D array.
  • We can convert a numpy array of 12 elements to a 2X6 matrix or 6X2 matrix or 4X3 matrix or 3&4 matrix.
  • If we try to convert it to a matrix of any other shape then it will raise an error,

Let’s checkout an example or incompatible conversion

Error,

We tried to create a matrix / 2D array of shape (3,2) i.e. 6 elements but our 1D numpy array had 9 elements only therefore it raised an error,

Using numpy.reshape() to convert a 1D numpy array to a 3D Numpy array

To convert a 1D Numpy array to a 3D Numpy array, we need to pass the shape of 3D array as a tuple along with the array to the reshape() function as arguments

We have a 1D Numpy array with 12 items,

Now let’s convert this 1D array to a 3D numpy array of shape (2X3X2),

Output:

Till now we have seen examples where we converted 1D array to either 2D or 3D. But using reshape() function we can convert an array of any shape to any other shape. Like,

Use numpy.reshape() to convert a 3D numpy array to a 2D Numpy array

Suppose we have a 3D Numpy array of shape (2X3X2),

Let’s convert this 3D array to a 2D array of shape 2X6 using reshape() function,

Output:

Use numpy.reshape() to convert a 2D numpy array to a 1D Numpy array

Suppose we have a 2D Numpy array of shape (3X3),

Let’s convert this 2D array to a 1D array,

Output:

If we pass -1 as the shape argument to the function reshape() then it will convert the array of any shape to a flat array.

numpy.reshape() returns a new view object if possible

Whenever possible numpy.reshape() returns a view of the passed object. If we modify any data in the view object then it will be reflected in the main object and vice-versa. Let’s understand this with an example,

Suppose we have a 1D numpy array,

let’s create a 2D view object of the 1D Numpy array using reshape(),

Output:

Now modify the second element in the original 1D numpy array.

Although we modified the 1D array only but this change should be visible in the 2D view object too. Let’s confirm this,

Output:

This proves that in the above example reshape() returned a view object. But there might be scenarios when reshape() would not be able to return a view object. But how to identify if the returned value is a view or not ?

How to check if reshape() returned a view object ?

Whatever object reshape() returns, we can check its base attribute to confirm if its view or not. If base attribute is None then it is not a view object, whereas if is not None then it is a view object and base attributes points to the original array object i.e.,

Output:

numpy.reshape() & different type of order parameters

In the reshape() function we can pass the order parameter too and its value can be ‘C’ o ‘F’ or ‘A’. Default value is ‘C’ . This parameter decides the order in which elements from the input array will be used.

Let’s understand this with an example,

Suppose we have a 1D array,

Convert 1D to 2D array row wise with order ‘C’

When we pass the order parameter as ‘C’ (default value of order parameter), then items from input array will be read row wise i.e.

Output:

Items from 1D array were read row wise i.e.

  • For 1st row of 2D array items from index 0 to 2 were read
  • For 2nd row of 2D array items from index 3 to 5 were read
  • For 2nd row of 2D array items from index 5 to 8 were read

Convert 1D to 2D array column wise with order ‘F’

When we pass the order parameter as ‘F’, then items from input array will be read column wise i.e.

Output:

Items from 1D array were read column wise i.e.

  • For 1st Column of 2D array items from index 0 to 2 were read
  • For 2nd Column of 2D array items from index 3 to 5 were read
  • For 2nd Column of 2D array items from index 5 to 8 were read

Convert 1D to 2D array by memory layout with parameter order “A”

Both ‘C’ and ‘F’ options do not consider the memory layout of the input array. Whereas, when we pass the order parameter as ‘A’, then items from the input array will be read based on internal memory layout. Let’s understand by an example,

Create a 1D numpy array

Create a 2D view object from this 1D numpy array and then get a transpose view object of that 2D array,

Output:

Now let’s convert this transposed view object to a 1D numpy array using order ‘C’ i.e. row wise based on current shape.

Output:

It read the elements row wise from the current shape of the view object and the memory layout of the original array was not considered. Now let’s convert this transposed view object to a 1D numpy array using order ‘A’ i.e. based on memory layout of original array,

Output:

It reads the elements row wise from the based on the memory layout of the original 1D array. It does not consider the current view of the input array i.e. a view object.

Convert the shape of a list using numpy.reshape()

In reshape() function we can pass a list too instead of array.For example, let’s use reshape() function to convert a list to 2D numpy array,

Output:

Now let’s convert this 2D numpy array to a list of list,

Output:

The complete example is as follows,

Output

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.