scikit-learn Preprocessing学习笔记(二)

时间:2022-08-22 23:51:29

Preprocessing是Python scikit learn第六个模块,主要介绍了如何对于数据集的预处理。

以下内容包含了一些个人观点和理解,如有疏漏或错误,欢迎补充和指出。

Should I standardize the input cases (row vectors)?

Whereas standardizing variables is usually beneficial, the effect of standardizing cases (row vectors) depends on the particular data. Cases are
typically standardized only across the input variables, since including the target variable(s) in the standardization would make prediction impossible

这句话的意思其实是问要不要对每一个样本做归一化。

对样本做归一化常用的有如下方式:

1)除以L2、L1norm,或者序列的和(非负时和L1norm相同),算数平均值,几何平均值等等

2)减去一个数值,常用算数平均值

对样本做归一化,事先要明白的是无论我们做减法或者除法,这都相当于减掉了样本的部分信息,如果你确定这部分信息是多余的,那么你可不必担心;如果这部分信息是有用处的,那么就不要对样本做这样的归一化。

举几个例子

图像处理中,图像的光照和对比度,前者相当于对每个像素点加一常量,后者相当于对每个像素点乘以一常量。有时候我们并不关心常量上的变化,比如一幅画可能你不是很在意白天观赏还是夜晚观赏。这里所谓的“在意”与“不在意”其实是相对于目标输出而言的,如果常量上的加法或者乘法对你的目标输出没有影响,那么你就可以减去所有像素点的平均值或者除以它们的标准差。这对于你研究的问题是没有很大影响的。

另外一个例子是你要判断某些植物属于哪个物种,你测量了它们的茎、干、叶。然后打算用神经网络的办法去判断它属于哪个物种。但是实际中你发现,由于年龄和生长环境的不同,即便是同一物种,它们的茎、干、叶也经常是不一样的。这类似于上个例子的光照或对比度。一个成熟的神经网络当然可以自主的判断上述植物的类别,但起主要作用的可能是它的形状,而非具体的数值。

基于以上两个例子,可以推断,不管是光照和对比度,还是茎、干、叶,在我们不关心它们多余成分的时候,完全可以对样本进行归一化处理。

如果数据是比率的,例如图像的对比度。你可以对样本做如下的归一化:

1)除和、算数平均值、几何平均值、标准差等

2)取对数,然后再减去算数平均值

3)除L2、L1norm。前者相当于把点映射到(超)球面,只有方向的差别,模长都是1.后者相当于把点映射到一个(超)平面上。当维度不高,而且随机变量非负的情况下,二者相差不大,有负数情况下二者相差较大。

Kohonen networks来讲,Kohonen networks的具体算法可以在网络上找到。大致的意思我可以做个不正式的比喻:

有一家公司来学校招聘,你和你的同学一起去听宣讲,公司可能包含多个部门,比如人事,公关,技术等等,这些都算不同的类别。接下来会多个有来自不同部门的人员到前面介绍自己的部门,如果某一人员的话很吸引你,你可能就要向他所在的部门走得更近些。当然,出于陪同的作用,你可能要带上你的同学跟你一起走近他,你的同学就代表你的相邻节点。就这样,随着以上过程不断的重复,最终你的位置总要最接近一个部门,这个过程你可能越来越不在意你的同学是否在陪你,可能处于考虑谨慎,你可能走得越来越慢,不过这些都不会影响你最终最接近一个部门。类似的过程不止发生在你身上,在场的其他人也都一样。于是人群分聚成了三组,分别想要应聘人事,公关,技术。这是聚类的过程,然后当再有人进来招聘的时候他就明白该去哪组人找有意愿的人。

当然,我要讲的其实不是Kohonen networks算法,算法可以是多种多样的,我想说算法中用到了一个东西,就是相似度的衡量。两个向量,在向量空间中,它们可以计算欧氏距离,数值小代表近,数值大代表远。考虑投影,向量A和任意单位向量I,做内积,投影大代表A和I近,投影小代表远。自然而言的,计算欧式距离,不必要做归一化,而计算投影,需要做归一化。

Should I standardize the variables (column vectors) for unsupervised learning?

对于无监督学习而言,若是单纯的想找自然类心,那么不建议进行标准化,而其他基于欧氏距离等相似度度量的无监督学习算法,可以进行标准化。

Should I standardize the target variables (column vectors)?
So it is essential to rescale the targets so that their variability reflects their importance, or at least is not in inverse relation to their importance. If the targets are of equal importance, they should typically be standardized to the same range or the same standard deviation. 

The scaling of the targets does not affect their importance in training if you use maximum likelihood estimation and estimate a separate scale parameter (such as a standard deviation) for each target variable. In this case, the importance of each target is inversely related to its estimated scale parameter. In other words, noisier targets will be given less importance.

For weight decay and Bayesian estimation, the scaling of the targets affects the decay values and prior distributions. Hence it is usually most convenient to work with standardized targets.
在权值衰减和贝叶斯估计器中,目标值(通常是y值),标准化会影响衰减值和先验或者后验分布,这时进行标准化会带来便利。
If you are standardizing targets to equalize their importance, then you should probably standardize to mean 0 and standard deviation 1, or use related robust estimators, as discussed under Should I standardize the input variables (column vectors)? If you are standardizing targets to force the values into the range of the output activation function, it is important to use lower and upper bounds for the values, rather than the minimum and maximum values in the training set. For example, if the output activation function has range [-1,1], you can use the following formulas: And of course, you apply the inverse of the standardization formula to the network outputs to restore them to the scale of the original target values. 
If the target variable does not have known upper and lower bounds, it is not advisable to use an output activation function with a bounded range. You can use an identity output activation function or other unbounded output
activation function instead; see Why use activation functions?