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.
It is often useful to normalise data. One type of normalisation is to 'zero
center' the data by subtracting it's mean. This will yield data with a new mean
of zero. Often we want to do this separately for each column or row of a
matrix. Also it's often the case that we want to perform this operation on a
square matrix. In that case, we can multiply by a 'centering matrix':
Here the matrix $C$ had the effect of subtracting $1/3$ from the first
column, subtracting $2/3$ from the second column, and adding $1$ to the last
column - ensuring that all columns sum to zero.
We create a function to build the centering matrix:
Code implementation
We then define a function that can zero center each column, each row, or
that matrix as a whole (if axis=2). If the matrix is square, then we use the
centering matrix approach above. Otherwise, after taking the mean across the
relevant axis (columns, rows, or all elements), we make a matrix of the same
size but filled with the mean of each axis and subtract it off the original
matrix:
Demo
We create a matrix, call the zero center 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):