Skip to main content

13.4) Useful Functions


You’ll often want to calculate the sum, minimum or maximum of an array:

>>> a = np.arange(15).reshape(3,5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> a.sum()
105
>>> a.min()
>>> a.max()
14

You can also apply these functions to specific dimensions (axis) of an array:

>>> a.min(axis=0)      # min along first axis eg vertical
Array([ 0,  1,  2,  3,  4])
>>> a.cumsum(axis=1)   # cumulative sum along the 2nd axis (horizontal)
array([[ 0,  1,  3,  6, 10],
       [ 5, 11, 18, 26, 35],
       [10, 21, 33, 46, 60]])