eco*_*con 9 python numpy image-preprocessing
我在教程中看到(没有进一步的解释)我们可以将数据处理为零均值x -= np.mean(x, axis=0)并使用数据标准化x /= np.std(x, axis=0).任何人都可以在代码上详细说明这两个部分,我从文档中得到的只是np.mean计算算术平均值沿特定轴计算平均值np.std并为标准偏差计算.
Jon*_*ler 11
这就是所谓的zscore.
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])
Run Code Online (Sandbox Code Playgroud)
请遵循以下代码中的注释
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)
Run Code Online (Sandbox Code Playgroud)
根据给定的语法,我得出结论,您的数组是多维的。因此,我将首先讨论 x 只是线性数组的情况:
np.mean(x)将计算平均值,通过广播将从所有条目中减去 的 x-np.mean(x)平均值。相当于. 类似对于.xx -=np.mean(x,axis = 0)x = x-np.mean(x,axis = 0)x/np.std(x)
在多维数组的情况下,也会发生同样的情况,但您只需计算第一个“轴”的平均值,而不是计算整个数组的平均值。轴是numpy维度的意思。所以如果你x是二维的,那么np.mean(x,axis =0) = [np.mean(x[:,0], np.mean(x[:,1])...]. 再次广播将确保对所有元素执行此操作。
请注意,这仅适用于第一个维度,否则形状将无法匹配广播。如果您想标准化另一个轴,您需要执行以下操作:
x -= np.expand_dims(np.mean(x, axis = n), n)
Run Code Online (Sandbox Code Playgroud)