Matthew Bennett

Logo

I am a data scientist working on time series forecasting (using R and Python 3) at the London Ambulance Service NHS Trust. I earned my PhD in cognitive neuroscience at the University of Glasgow working with fmri data and neural networks. I favour linux machines, and working in the terminal with Vim as my editor of choice.

View my GitHub Profile

View my LinkedIn Profile

View my CV

Sum and mean

Sum

Summing all the values in each column, or in each row, of a matrix is a common operation. We can achieve it my multiplying the matrix with a vector of ones: multiplying on the left side will sum the columns, multiplying on the right will sum the rows.

Code implementation

In our function, we will always multiply on the left, and therefore transpose the matrix in the case that we want the sum the rows. If a vector is passed, we always sum the elements:

def sum(A, axis=0):
    # if we have a vector, we sum along it's length
    if min(la.size(A)) == 1:
        axis = la.size(A).index(max(la.size(A)))
    if axis == 1:
        A = A.tr()
    ones = la.gen_mat([1,la.size(A)[0]],1)
    A_sum = ones.multiply(A)
    return A_sum

Demo

We create a matrix, call the sum method twice with a different axis as an argument and print the results:

import linalg as la

A = la.Mat([[1, 2, 3],
            [-2, 1, 4],
            [0, 1, 2],
            [3, 6, 1]])

result = la.stats.sum(A)
la.print_mat(result)

result = la.stats.sum(A, axis=1)
la.print_mat(result)

Outputs:
>>> la.print_mat(result)
[2, 10, 10]

>>> la.print_mat(result)
[6, 3, 3, 10]

Mean

Now that we have a function to sum, we can divide the result by the number of elements that went into the sum to obtain the mean. We transpose the vector from a row to a column in the case that we computed the mean across the rows of the matrix:

def mean(A, axis=0):
    # if we have a vector, we take the mean along it's length
    if min(la.size(A)) == 1:
        axis = la.size(A).index(max(la.size(A)))
    A_sum = sum(A, axis)
    A_mean = A_sum.div_elwise(la.size(A)[axis])
    if axis == 1:
        A_mean = A_mean.tr()
    return A_mean

Demo

We create a matrix, call the mean method twice with a different axis as an argument and print the results (we only print 2 decimal places in one case to make it look pretty):

import linalg as la

A = la.Mat([[1, 2, 3],
            [-2, 1, 4],
            [0, 1, 2],
            [3, 6, 1]])

la.print_mat(la.stats.mean(A))

la.print_mat(la.stats.mean(A, axis=1), 2)

Outputs:

>>> la.print_mat(la.stats.mean(A))
[0.5, 2.5, 2.5]

>>> la.print_mat(la.stats.mean(A, axis=1), 2)
[2.0]
[1.0]
[1.0]
[3.33]
Zero-center >

back to project main page
back to home