# Python NumPy For Your Grandma - 3.3 reshape()

Contents

In this section, we’ll see how to use the reshape() function to change the shape of an array.

Suppose we have 1d array called foo with eight elements.

import numpy as np

foo = np.arange(start = 1, stop = 9)  # [1, 2, 3, 4, 5, 6, 7, 8]
print(foo)
## [1 2 3 4 5 6 7 8]

We can reshape foo into a 2x4 array using either the .reshape() method of the array object,

foo.reshape(2,4)
## array([[1, 2, 3, 4],
##        [5, 6, 7, 8]])

or the free function np.reshape().

np.reshape(a = foo, newshape = (2,4))
## array([[1, 2, 3, 4],
##        [5, 6, 7, 8]])

These methods implement the same logic, just with slightly different interfaces. With foo.reshape(), we can pass in the new dimensions individually instead of as a tuple, but this comes at the expense of not being able to specify the newshape keyword.

Now let’s set bar equal to foo.reshape(2,4).

bar = foo.reshape(2,4)
print(bar)
## [[1 2 3 4]
##  [5 6 7 8]]

And then let’s reshape bar from a 2x4 array to 4x2 array. We can do this in two different orders. First, we’ll call bar.reshape() with order equal to ‘C'. This implements “C-style” or “row major” order which reorders the last axis first.

bar.reshape((4,2), order = 'C')
## array([[1, 2],
##        [3, 4],
##        [5, 6],
##        [7, 8]])

Alternatively, we can set order equal to ‘F’ This implements “Fortran-style” or “column major” order which reorders the first axis first.

bar.reshape((2,2,2), order = 'F')
## array([[[1, 3],
##         [2, 4]],
##
##        [[5, 7],
##         [6, 8]]])

Let’s see those again except this time we’ll reshape bar into a 2x2x2 array.

# C-style order
bar.reshape((2,2,2), order = 'C')
## array([[[1, 2],
##         [3, 4]],
##
##        [[5, 6],
##         [7, 8]]])
# Fortran-style order
bar.reshape((2,2,2), order = 'F')
## array([[[1, 3],
##         [2, 4]],
##
##        [[5, 7],
##         [6, 8]]])

Quick tip - when you reshape an array, you can use -1 for exactly one of the newshape dimensions and NumPy will calculate it for you. For example, if we do bar.reshape(4, -1), and Numpy knows to reshape bar into a 4x2 array.

bar.reshape(4, -1)
## array([[1, 2],
##        [3, 4],
##        [5, 6],
##        [7, 8]])

Another thing to note is that in all these examples, we’re creating a copy of the existing array. So, when we do bar.reshape(4, 2), it doesn’t actually modify bar - it just creates a new array with new data. If you wanted the reshape to stick, you could do bar = bar.reshape(4,2) but this still creates an unnecessary copy. So a better way to do this is just to explicitly set bar.shape = (4,2).

bar.shape = (4,2)
print(bar)
## [[1 2]
##  [3 4]
##  [5 6]
##  [7 8]]

And then the last thing I want to cover here is the array transpose. So, if you want to get the transpose of bar, you can do bar.T or np.transpose(bar).

bar.T
## array([[1, 3, 5, 7],
##        [2, 4, 6, 8]])

And the way to interpret this in higher dimensions is, let’s say you have a 2x3x4x5 array. This transposes into a 5x4x3x2 array where element (i,j,k,l) in the original array maps to element (l,k,j,i) in the transposed array.