Numpy:零均值数据与标准化

2024-06-06 22:58:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在教程中看到(没有进一步的解释),我们可以用x -= np.mean(x, axis=0)将数据处理为零平均值,并用x /= np.std(x, axis=0)规范化数据。有人能详细说明这两段代码吗,我从文献中得到的唯一结论是,np.mean计算算术平均值计算沿特定轴的平均值,np.std计算标准差。


Tags: 数据代码np教程算术mean规范化文献
3条回答

遵循下面代码中的注释

import numpy as np

# create x
x = np.asarray([1,2,3,4], dtype=np.float64)

np.mean(x) # calculates the mean of the array x
x-np.mean(x) # this is euivalent to subtracting the mean of x from each value in x
x-=np.mean(x) # the -= means can be read as x = x- np.mean(x)

np.std(x) # this calcualtes the standard deviation of the array
x/=np.std(x) # the /= means can be read as x = x/np.std(x)

根据给定的语法,我得出结论,数组是多维的。因此,我将首先讨论x只是一个线性数组的情况:

np.mean(x)将计算平均值,通过广播x-np.mean(x)将从所有条目中减去x的平均值。x -=np.mean(x,axis = 0)相当于x = x-np.mean(x,axis = 0). Similar forx/np.std(x)`。

对于多维数组,同样的事情也会发生,但不是计算整个数组的平均值,而是计算第一个“轴”的平均值。轴是维度的numpy单词。所以如果你的x是二维的,那么np.mean(x,axis =0) = [np.mean(x[:,0], np.mean(x[:,1])...]。广播将再次确保,这是对所有元素。

注意,这只适用于第一个维度,否则形状将不匹配广播。如果要规范化wrt另一个轴,则需要执行以下操作:

x -= np.expand_dims(np.mean(x,axis = n),n)

这就是所谓的^{}

SciPy有一个实用程序:

>>> from scipy import stats
>>> stats.zscore([ 0.7972,  0.0767,  0.4383,  0.7866,  0.8091,
...                0.1954,  0.6307,  0.6599,  0.1065,  0.0508])
array([ 1.1273, -1.247 , -0.0552,  1.0923,  1.1664, -0.8559,  0.5786,
        0.6748, -1.1488, -1.3324])

相关问题 更多 >