8.1. Statistics Reduction

8.1.1. SetUp

>>> import numpy as np

8.1.2. Sum

  • a.sum()

  • np.sum()

  • np.nansum()

>>> a = np.array([[1.1, 2.2, 3.3],
...               [4.4, 5.5, 6.6]])
>>>
>>>
>>> a.sum()
np.float64(23.1)
>>>
>>> a.sum(axis=0)
array([5.5, 7.7, 9.9])
>>>
>>> a.sum(axis=1)
array([ 6.6, 16.5])
>>>
>>> np.sum(a)
np.float64(23.1)
>>> a = np.array([[1.1, 2.2, 3.3],
...               [4.4, np.nan, 6.6]])
>>>
>>>
>>> a.sum()
np.float64(nan)
>>>
>>> np.sum(a)
np.float64(nan)
>>>
>>> np.nansum(a)
np.float64(17.6)

8.1.3. Cumulative Sum

  • a.cumsum()

  • np.cumsum()

  • np.nancumsum()

>>> a = np.array([[1.1, 2.2, 3.3],
...               [4.4, 5.5, 6.6]])
>>>
>>>
>>> a.cumsum()
array([ 1.1,  3.3,  6.6, 11. , 16.5, 23.1])
>>>
>>> np.cumsum(a)
array([ 1.1,  3.3,  6.6, 11. , 16.5, 23.1])
>>> a = np.array([[1.1, 2.2, 3.3],
...                [4.4, np.nan, 6.6]])
>>>
>>>
>>> a.cumsum()
array([ 1.1,  3.3,  6.6, 11. ,  nan,  nan])
>>>
>>> np.cumsum(a)
array([ 1.1,  3.3,  6.6, 11. ,  nan,  nan])
>>>
>>> np.nancumsum(a)
array([ 1.1,  3.3,  6.6, 11. , 11. , 17.6])

8.1.4. Product

  • a.prod()

  • np.prod()

  • np.nanprod()

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.prod()
np.int64(720)

8.1.5. Cumulative Product

  • a.cumprod()

  • np.cumprod()

  • np.nancumprod()

8.1.6. Mean

  • a.mean()

  • np.mean()

  • np.nanmean()

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.mean()
np.float64(3.5)
>>>
>>> a.mean(axis=0)
array([2.5, 3.5, 4.5])
>>>
>>> a.mean(axis=1)
array([2., 5.])

8.1.7. Cumulative Mean

  • a.cummean()

  • np.cummean()

  • np.nancummean()

8.1.8. Variance

  • a.var()

  • np.var()

  • np.nanvar()

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.var()
np.float64(2.9166666666666665)
>>>
>>> a.var(axis=0)
array([2.25, 2.25, 2.25])
>>>
>>> a.var(axis=1)
array([0.66666667, 0.66666667])

8.1.9. Standard Deviation

  • a.std()

  • np.std()

  • np.nanstd()

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.std()
np.float64(1.707825127659933)
>>>
>>> a.std(axis=0)
array([1.5, 1.5, 1.5])
>>>
>>> a.std(axis=1)
array([0.81649658, 0.81649658])

8.1.10. Minimal Value

  • np.ndarray.argmin() index of an np.ndarray.min() element in array

  • np.nanmin()

  • np.nanargmin()

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.min()
np.int64(1)
>>>
>>> a.min(axis=0)
array([1, 2, 3])
>>>
>>> a.min(axis=1)
array([1, 4])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.argmin()
np.int64(0)
>>>
>>> a.argmin(axis=0)
array([0, 0, 0])
>>>
>>> a.argmin(axis=1)
array([0, 0])
>>> a = np.array([[99,   2, 33],
...               [22,   0,  4],
...               [4,  155,  6]])
>>>
>>>
>>> a.min()
np.int64(0)
>>>
>>> a.min(axis=0)
array([4, 0, 4])
>>>
>>> a.min(axis=1)
array([2, 0, 4])
>>>
>>> a.min(axis=-1)
array([2, 0, 4])
>>>
>>>
>>> a.argmin()
np.int64(4)
>>>
>>> a.argmin(axis=0)
array([2, 1, 1])
>>>
>>> a.argmin(axis=1)
array([1, 1, 0])
>>>
>>> a.argmin(axis=-1)
array([1, 1, 0])
>>>
>>>
>>> a.flat[4]
np.int64(0)
>>>
>>> np.unravel_index(4, (3, 3))
(np.int64(1), np.int64(1))
>>>
>>> np.unravel_index(a.argmin(), a.shape)
(np.int64(1), np.int64(1))

Shows the coordinates of argmin value:

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.min()
np.int64(1)
>>>
>>> a.argmin()
np.int64(0)
>>>
>>> np.unravel_index(a.argmin(), a.shape)
(np.int64(0), np.int64(0))
>>>
>>> a == a.min()
array([[ True, False, False],
       [False, False, False]])

8.1.11. Maximal Value

  • np.ndarray.argmax() index of an a.max() element in array

  • np.nanmax()

  • np.nanargmax()

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.max()
np.int64(6)
>>>
>>> a.max(axis=0)
array([4, 5, 6])
>>>
>>> a.max(axis=1)
array([3, 6])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.argmax()
np.int64(5)
>>>
>>> a.argmax(axis=1)
array([2, 2])
>>>
>>> a.argmax(axis=0)
array([1, 1, 1])
>>> a = np.array([[99,   2, 33],
...               [22,   0,  4],
...               [4,  155,  6]])
>>>
>>>
>>> a.max()
np.int64(155)
>>>
>>> a.max(axis=0)
array([ 99, 155,  33])
>>>
>>> a.max(axis=1)
array([ 99,  22, 155])
>>>
>>> a.max(axis=-1)
array([ 99,  22, 155])
>>>
>>>
>>> a.argmax()
np.int64(7)
>>>
>>> a.argmax(axis=0)
array([0, 2, 0])
>>>
>>> a.argmax(axis=1)
array([0, 0, 1])
>>>
>>> a.argmax(axis=-1)
array([0, 0, 1])
>>>
>>> a.flat[7]
np.int64(155)
>>>
>>> np.unravel_index(7, (3, 3))
(np.int64(2), np.int64(1))
>>>
>>> np.unravel_index(a.argmax(), a.shape)
(np.int64(2), np.int64(1))

Shows the coordinates of argmax value:

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>>
>>> a.max()
np.int64(6)
>>>
>>> a.argmax()
np.int64(5)
>>>
>>> np.unravel_index(a.argmax(), a.shape)
(np.int64(1), np.int64(2))
>>>
>>> a == a.max()
array([[False, False, False],
       [False, False,  True]])

8.1.12. Median

  • np.median()

  • np.nanmedian()

8.1.13. Quantile

  • np.quantile()

  • np.nanquantile()

8.1.14. Percentile

  • np.percentile()

  • np.nanpercentile()