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.

Mathematical functions

NumPy provides many mathematical functions that can be used in a vectorised manner.

Min and Max

You can use the ndarray methods .max() or .min() to compute the maximum/minimum values in an array.

np.max(arr) and np.min(arr) also works.

a = np.array([[4,7,3],[1,2,5]])

# Compute overall min/max
print(a.min())  ## 1
print(a.max())  ## 7

# Compute min/max across rows
print(a.min(axis=0))  ## [1 2 3]
print(a.max(axis=0))  ## [4 7 5]

# Compute min/max across columns
print(a.min(axis=1))  ## [3 1]
print(a.max(axis=1))  ## [7 5]

To get the indices for the min/max values, use .argmax() and .argmin().

a = np.array([[4,7,3],[1,2,5]])

# Get (flattened) indices of overall min/max
print(a.argmin())  ## 3 (index of "1")
print(a.argmax())  ## 1 (index of "7")

# Get row indices of min/max across rows
print(a.argmin(axis=0))  ## [1 1 0]
print(a.argmax(axis=0))  ## [0 0 1]

# Get column indices of min/max across columns
print(a.argmin(axis=1))  ## [2 0]
print(a.argmax(axis=1))  ## [1 2]

Statistical methods for arrays

  • arr.mean(axis=None)
    • Compute the mean of arr.
    • If axis is not provided, compute the mean of the flattened array
    • If axis is provided, compute the means across the axis.
  • arr.std(axis=None)
    • Compute the standard deviation of arr
    • If axis is not provided, compute the standard deviation of the flattened array
    • If axis is provided, compute the standard deviations across the axis.
  • arr.var(axis=None)
    • Compute the variance of arr
    • If axis is not provided, compute the variance of the flattened array
    • If axis is provided, compute the variances across the axis.

Sum and Cumulative Sum

Like .min() and .max(), .sum() can be used to sum up a flattened array, or compute the sum across a specified axis. Explore the output for below yourself and try to make sense of it! Draw out the 3D array and the axis direction on a piece of paper if you are confused.

a = np.arange(24).reshape((2,3,4))
print(a)
## [[[ 0  1  2  3]
##   [ 4  5  6  7]
##   [ 8  9 10 11]]
##
##  [[12 13 14 15]
##   [16 17 18 19]
##   [20 21 22 23]]]

print(a.sum())
## 276

print(a.sum(axis=0))
## [[12 14 16 18]
##  [20 22 24 26]
##  [28 30 32 34]]

print(a.sum(axis=1))
## [[12 15 18 21]
##  [48 51 54 57]]

print(a.sum(axis=2))
## [[ 6 22 38]
##  [54 70 86]]

.cumsum() computes the cumulative sum of an array.

a = np.array([1, 2, 3, 4, 5])
print(a.cumsum())  ### [1 3 6 10 15]

Like .sum(), you can compute the cumulative sum across a specific axis.

a = np.array([[1,2,3], [4,5,6]])
print(a)
## [[1 2 3]
##  [4 5 6]]

print(a.cumsum())
## [ 1  3  6 10 15 21]
 
print(a.cumsum(axis=0))
## [[1 2 3]
##  [5 7 9]]

print(a.cumsum(axis=1))
## [[ 1  3  6]
##  [ 4  9 15]]

Product and Cumulative Product

Just like .sum() and .cumsum(), .prod() amd .cumsum() computes the product and cumulative product of an array respectively.

I will not provide examples for these as they are essentially similar to the above (just multiply instead of adding). And frankly, are you bored reading these just as I am bored writing these by now?🥱

Others

These functions can be applied to arrays in an elementwise fashion.

  • np.floor(arr): Floor function
  • np.round(arr): Round function
  • np.exp(arr): Exponential function
  • np.sqrt(arr): Square root
  • np.sin(arr): Sin function
  • np.cos(arr): Cos function