Contents

Python NumPy For Your Grandma - 3.2 newaxis

In this section, we’ll see how to use the newaxis keyword to increase the dimensionality of an array and why that’s useful.

Suppose you have these two arrays, A and B, and your goal is to build a 4x3 difference matrix where element ij gives A_i - B_j. In other words, your goal is to subtract each element of B from each element of A.

import numpy as np

A = np.array([3, 11, 4, 5])
print(A)
## [ 3 11  4  5]
B = np.array([5, 0, 3])
print(B)
## [5 0 3]

If you do A minus B, you’ll get an error because the arrays don’t have compatible shapes and even if they were the same size, numpy would just do element-wise subtraction which isn’t what we want.

However, imagine if A was a 4x1 array and B was 1x3 array. In this case, numpy would use broadcasting to, in essence, turn them both into 4x3 arrays, and then carry out the element-wise subtraction that we want. So the question is, how do we convert A into a 4x1 array and B into a 1x3 array?

Surprise, surprise - you can use np.newaxis for that. So if we do A[:, np.newaxis], it inserts a new axis behind A’s existing axis. In other words, it converts A from a (4.) array into a (4,1) array.

A[:, np.newaxis]
## array([[ 3],
##        [11],
##        [ 4],
##        [ 5]])

And then if we do B[np.newaxis, :], it inserts a new axis in front of B’s existing axis. In other words, it converts B from a (3,) array into a (1,3) array.

B[np.newaxis, :]
## array([[5, 0, 3]])

And now we can just carry out the subtraction to get our desired difference matrix.

A[:, np.newaxis] - B[np.newaxis, :]
## array([[-2,  3,  0],
##        [ 6, 11,  8],
##        [-1,  4,  1],
##        [ 0,  5,  2]])

Note that newaxis is just an alias for the None keyword, so you’ll often see people, including myself, use the None keyword here.

A[:, None] - B[None, :]
## array([[-2,  3,  0],
##        [ 6, 11,  8],
##        [-1,  4,  1],
##        [ 0,  5,  2]])

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