Contents

Python NumPy For Your Grandma - 3.3 reshape()

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.


Course Curriculum

  1. Introduction
    1.1 Introduction
  2. Basic Array Stuff
    2.1 NumPy Array Motivation
    2.2 NumPy Array Basics
    2.3 Creating NumPy Arrays
    2.4 Indexing 1-D Arrays
    2.5 Indexing Multidimensional Arrays
    2.6 Basic Math On Arrays
    2.7 Challenge: High School Reunion
    2.8 Challenge: Gold Miner
    2.9 Challenge: Chic-fil-A
  3. Intermediate Array Stuff
    3.1 Broadcasting
    3.2 newaxis
    3.3 reshape()
    3.4 Boolean Indexing
    3.5 nan
    3.6 infinity
    3.7 random
    3.8 Challenge: Love Distance
    3.9 Challenge: Professor Prick
    3.10 Challenge: Psycho Parent
  4. Common Operations
    4.1 where()
    4.2 Math Functions
    4.3 all() and any()
    4.4 concatenate()
    4.5 Stacking
    4.6 Sorting
    4.7 unique()
    4.8 Challenge: Movie Ratings
    4.9 Challenge: Big Fish
    4.10 Challenge: Taco Truck
  5. Advanced Array Stuff
    5.1 Advanced Array Indexing
    5.2 View vs Copy
    5.3 Challenge: Population Verification
    5.4 Challenge: Prime Locations
    5.5 Challenge: The Game of Doors
    5.6 Challenge: Peanut Butter
  6. Final Boss
    6.1 as_strided()
    6.2 einsum()
    6.3 Challenge: One-Hot-Encoding
    6.4 Challenge: Cumulative Rainfall
    6.5 Challenge: Table Tennis
    6.6 Challenge: Where’s Waldo
    6.7 Challenge: Outer Product

Additional Content

  1. Python Pandas For Your Grandpa
  2. Neural Networks For Your Dog
  3. Introduction To Google Colab