3.17. Array Statistics

3.17.1. Sum

import numpy as np


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

a.sum()
# 21

a.sum(axis=0)
# array([5, 7, 9])

a.sum(axis=1)
# array([ 6, 15])

3.17.2. Product

import numpy as np


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

a.prod()
# 720

3.17.3. Mean

import numpy as np


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

a.mean()
# 3.5

a.mean(axis=0)
# array([2.5, 3.5, 4.5])

a.mean(axis=1)
# array([2., 5.])

3.17.4. Variance

import numpy as np


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

a.var()
# 2.9166666666666665

a.var(axis=0)
# array([2.25, 2.25, 2.25])

a.var(axis=1)
# array([0.66666667, 0.66666667])

3.17.5. Standard Deviation

import numpy as np


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

a.std()
# 1.707825127659933

a.std(axis=0)
# array([1.5, 1.5, 1.5])

a.std(axis=1)
# array([0.81649658, 0.81649658])

3.17.6. Minimal Value

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

import numpy as np


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

a.min()
# 1

a.min(axis=0)
# array([1, 2, 3])

a.min(axis=1)
# array([1, 4])
import numpy as np


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

a.argmin()
# 0

a.argmin(axis=0)
# array([0, 0, 0])

a.argmin(axis=1)
# array([0, 0])
Listing 514. Shows the coordinates of argmin value
import numpy as np


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

a.min()
# 1

a.argmin()
# 0

np.unravel_index(a.argmin(), a.shape)
# (0, 0)

a == a.min()
# array([[ True, False, False],
#        [False, False, False]])

3.17.7. Maximal Value

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

import numpy as np


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

a.max()
# 6

a.max(axis=0)
# array([4, 5, 6])

a.max(axis=1)
# array([3, 6])
import numpy as np


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

a.argmax()
# 5

a.argmax(axis=1)
# array([2, 2])

a.argmax(axis=0)
# array([1, 1, 1])
Listing 515. Shows the coordinates of argmax value
import numpy as np


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

a.max()
# 6

a.argmax()
# 5

np.unravel_index(a.argmax(), a.shape)
# (1, 2)

a == a.max()
# array([[False, False, False],
#        [False, False,  True]])

3.17.8. Assignments

Todo

Create assignments