Calculate Average, Variance, Standard Deviation of a Matrix in Numpy – Numpy Tutorial

By | September 15, 2019

The average of a matrix is simple, however, how to calculate variance and standard deviation of a matrix?

Variance is defined as:

variance formula

Standard deviation is defined as:

Standard Deviation

Here is an example to show how to calculate them.

Preliminaries

  1. import numpy as np
import numpy as np

Create a matrix

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

The output is:

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

Calculate the average of this matrix

  1. avg = np.mean(m)
 avg = np.mean(m)

The output is 3.5

Calculate the variance

  1. var = np.var(m)
var = np.var(m)

The output is 2.9166666666666665

Calculate standard deviation

  1. std = np.std(m)
std = np.std(m)

The output is 1.707825127659933

Leave a Reply