Variance and standard | Python3 program to find mean and variance of a matrix . ; variance function declaration Function for calculating mean ; Calculating sum ; Returning mean ; Function for calculating variance ; subtracting mean from elements ; a [ i ] [ j ] = fabs ( a [ i ] [ j ] ) ; squaring each terms ; taking sum ; declaring and initializing matrix ; for mean ; for variance ; for standard deviation ; displaying variance and deviation
import math ; NEW_LINE def mean ( a , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT sum += a [ i ] [ j ] ; NEW_LINE DEDENT DEDENT return math . floor ( int ( sum / ( n * n ) ) ) ; NEW_LINE DEDENT def variance ( a , n , m ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT a [ i ] [ j ] -= m ; NEW_LINE a [ i ] [ j ] *= a [ i ] [ j ] ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT sum += a [ i ] [ j ] ; NEW_LINE DEDENT DEDENT return math . floor ( int ( sum / ( n * n ) ) ) ; NEW_LINE DEDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; NEW_LINE m = mean ( mat , 3 ) ; NEW_LINE var = variance ( mat , 3 , m ) ; NEW_LINE dev = math . sqrt ( var ) ; NEW_LINE print ( " Mean : " , m ) ; NEW_LINE print ( " Variance : " , var ) ; NEW_LINE print ( " Deviation : " , math . floor ( dev ) ) ; NEW_LINE