Numpy:zero mean data and standardization

后端 未结 4 1302
既然无缘
既然无缘 2021-02-19 19:08

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

4条回答
  •  滥情空心
    2021-02-19 20:09

    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 forx/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)
    

提交回复
热议问题