This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 6: NumPy functions

Mathematical functions

face Josiah Wang

Let’s get back to being practical!

Your task now is to take a quick glance at all the lovely mathematical functions provided by NumPy in the documentation. These can be used in a vectorised manner (i.e. applied to whole np.arrays without loops).

Confusingly, many (but not all) of these are also methods of np.array. so both np.mean(x) and x.mean() work! I personally prefer the OOP version as it is shorter.

These are all pretty straightforward and self-explanatory though. So rather than me listing out all the possible functions, I’ll just let you do a quick quiz to try to explore some of these yourself!

1 2 3 4 5 6 7

Question 1

Write a function/method call to compute the standard deviation of x below. Your answer should look something like x.sum() or np.sum(x).

>>> import numpy as np
>>> x = np.array([1, 5, 3, 2, 3, 6])
>>> y = ?????
>>> print(y)
1.699673171197595
x.std()
Explanation:

A straightforward one: you can use either x.std() or np.std(x). You can also use np.sqrt(x.var()) if you really want!

Question 2

Write a function/method call to compute the median of x below. Your answer should look something like x.sum() or np.sum(x).

>>> import numpy as np
>>> x = np.array([1, 5, 3, 2, 3, 6])
>>> y = ?????
>>> print(y)
3.0
np.median(x)
Explanation:

Use np.median(x) to compute the median. Unfortunately there is no .median() method, so you cannot use x.median()!

Question 3

Write a function/method call to compute the product of x below (i.e. multiply all numbers in x). Your answer should look something like x.sum() or np.sum(x).

>>> import numpy as np
>>> x = np.array([1, 3, 2, 4])
>>> y = ?????
>>> print(y)
24
x.prod()
Explanation:

Use x.prod() and np.prod(x) to multiply all numbers in x!

Question 4

Write a function/method call to compute the sum of each column in x below. Your answer should look something like x.sum() or np.sum(x).

>>> import numpy as np
>>> x = np.array([[7, 1, 3], [9, 3, 6]])
>>> y = ?????
>>> print(y)
[16 4 9]
x.sum(axis=0)
Explanation:

You can sum up numbers along a specified axis if you provide the axis argument. So x.sum(axis=0) adds up each column, and x.sum(axis=1) adds up each row ([11 18]). The function equivalent np.sum(x, axis=0) also works similarly.

Question 5

What is the output of the following piece of code? Write out your answer as how a np.array would be printed, e.g. [1 2 3].

import numpy as np
x = np.array([[4, 7, 3], [1, 2, 5]])
y = x.argmin(axis=1)
print(y)
[2 0]
Explanation:

The x.argmin() method (or equivalently np.argmin(x) function) gives the index of the minimum value in the array x.

If the axis argument is not provided, then it will give the index of the minimum across the whole flattened array (3, pointing to x[1,0]).

If axis=0, it computes the indices of the minimum for each column ([1 1 0] in this case, since [1 2 3] are the minimum values for each column).

If axis=1, it computes the indices of the minimum for each row, so [2 0] for this case since the minimum is 3 in row 0 and 1 in row 1.

Question 6

What is the output of the following piece of code? Write out your answer as how a np.array would be printed, e.g. [1 2 3].

import numpy as np
x = np.array([1, 2, 3])
y = x.cumsum()
print(y)
[1 3 6]
Explanation:

The x.cumsum() method (or equivalently np.cumsum(x) function) gives the cumulative sum of x. So it will return [1 1+2 1+2+3] which is [1 3 6].

Question 7

What is the output of the following piece of code? Write out your answer as how a np.array would be printed, e.g. [1 2 3].

import numpy as np
x = np.array([[3, 0, 2], 
              [2, 4, 5], 
              [1, 6, 7]])
y = x.diagonal()
print(y)
[3 4 7]
Explanation:

The x.diagonal() method (or equivalently np.diagonal(x) function) returns the diagonal of x. In this case, it returns [3 4 7].

You can also extract the off-diagonals by providing an offset, e.g. x.diagonal(offset=1) will give you [0 5], and x.diagonal(offset=-1) will return [2 6].