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

import numpy as np

Create a matrix

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

The output is:

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

Calculate the average of this matrix

 avg = np.mean(m)

The output is 3.5

Calculate the variance

var = np.var(m)

The output is 2.9166666666666665

Calculate standard deviation

std = np.std(m)

The output is 1.707825127659933