This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

NumPy exercises

Now that you have spent so much time reading, it is time to get your hands dirty with NumPy!

Many thanks to the following people for contributing to these exercises: Joe Stacey

To use NumPy, please type ‘import numpy as np’ into the Python console. We will assume that you have already installed NumPy (if not please see previous module).

Part A:

Ex1

a) Try to multiply each element of list(range(10)) by 2, then minus 3 from each element

NumPy provides a much easier solution. We will try to do this the NumPy way:

b) Now multiply each element of np.arange(10) by 2
  • You’ll see this is much easier using NumPy arrays.

Ex2: Find out how you would do the following using NumPy (try making some educated guesses before looking anything up!):

  • Create an identity matrix
  • Find the square root of each element in an array
  • Square each element in an array
  • Take logs of each element in an array
  • Create an array of zeros
  • Create an array of ones
  • Give the value of Pi
  • Transpose an array
  • Round down a float
  • Find the median of floats in a NumPy array
  • Add a new element to a NumPy array

Ex3: Create a 1x40 1D array, and then convert this into a 8x5 2d NumPy array

Ex4: Create a 1x2000 array with the step wise sequence from 1 to 2000, and then convert this into a 40x50 2d NumPy array. Then:

  • Index this array correctly to return the value 435
  • Index this array correctly to return the value 951

Ex5a: What happens when we use .ravel()?

  • For example: How is np.arange(20).reshape(20,1).ravel() different to np.arange(20).reshape(20,1)?
  • Try using .shape to help investigate

Ex5b: We can use .all() to check whether two NumPy arrays are identical. Which of the following do you expect to return True? Check your answers using Python:

  • (np.arange(20).reshape(20,1) == np.arange(20)).all()
  • (np.arange(20).reshape(20,1).ravel() == np.arange(20)).all()
  • (np.arange(20).reshape(20,1,1).ravel() == np.arange(20)).all()
  • (np.arange(20).reshape(20,) == np.arange(20)).all()
  • (np.arange(20).reshape(20,) == np.arange(20).ravel()).all()

Ex6: Can you find 2 different ways to do element-wise multiplication between two arrays?

  • Try using a = np.arange(9).reshape(3, 3)

Ex7: Can you find 3 different ways to do matrix multiplication between two arrays?

Ex8: Normalise the vector reprsented by np.array([3, 5, 1, 2, 4]) to find the unit vector in the same direction.

Challenge questions:

Ex9: Find the angle between the two vectors a and b represented by:

  • a = np.array([3, 4, 1, 5])
  • b = np.array([4, 1, 1, -1])

Ex10: Use NumPy and matrix multipliation to solve the following linear equations:

  • 2x + 4y = 8
  • 3x - 2y = 8

Ex11: Solve the following equation to find w given X and y:

\(w = (X^{t} X) X^{t} y\)

  • Where X: np.array([[1, 2, 3], [2, 4, 5]]) and y = np.array([3, 5])

Mega challenge question:

Ex12: Using as few lines of code as possible, create an 8x8 NumPy array with a checkboard pattern

e.g. [[0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0]… etc

  • As an extension exercise, can you create a program that will generalise this to create a checkboard pattern with an nxn checkerboard for any given n?