I saw in tutorial (there were no further explanation) that we can process data to zero mean with x -= np.mean(x, axis=0)
and normalize data with x /= np.std(x
From the given syntax you have I conclude, that your array is multidimensional. Hence I will first discuss the case where your x is just a linear array:
np.mean(x)
will compute the mean, by broadcasting x-np.mean(x)
the mean of x
will be subtracted form all the entries. x -=np.mean(x,axis = 0)
is equivalent to x = x-np.mean(x,axis = 0). Similar for
x/np.std(x)`.
In the case of multidimensional arrays the same thing happens, but instead of computing the mean over the entire array, you just compute the mean over the first "axis". Axis is the numpy
word for dimension. So if your x
is two dimensional, then np.mean(x,axis =0) = [np.mean(x[:,0], np.mean(x[:,1])...]
. Broadcasting again will ensure, that this is done to all elements.
Note, that this only works with the first dimension, otherwise the shapes will not match for broadcasting. If you want to normalize wrt another axis you need to do something like:
x -= np.expand_dims(np.mean(x,axis = n),n)