[Sklearn应用] Preprocessing data(一) 标准化(Standardization) 与 正则化/归一化(Normalization)

此内容在sklearn官网地址: http://scikit-learn.org/stable/modules/preprocessing.html#
sklearn版本:0.18.2

部分文字引用自: http://www.cnblogs.com/chaosimple/p/4153167.html

Standardization 标准化
Normalization 正则化

是否需要进行数据标准化?

一般涉及到梯度下降和距离的计算需要进行标准化或正则化。例如Logistic Regression、SVM、PCA等。

很多博客中将标准化和正则化归为同一种处理方式,实际上他们是不同的情况,标准化针对的是把整列(特征)作为处理对象,而正则化把行(样本)作为处理对象,而且他们处理的公式方法也不同。

标准化(Standardization)

Standardization of datasets is a common requirement for many machine learning estimators implemented in scikit-learn; they might behave badly if the individual features do not more or less look like standard normally distributed data: Gaussian with zero mean and unit variance. ——scikit-learn.org

标准化是常用的机器学习特征处理的方法,它可以将一列数据的平均值变为0,方差变为1 。但是不改变原来的数据结构分布,只是将数值进行了缩放。使得所有特征在同一量纲下进行数据处理,避免有些特征整体偏大对整体处理造成偏差。
z-score 方法这里写图片描述
μ \mu μ 是平均值, σ \sigma σ 是标准差。
得到的结果是,对于每个特征(每列),他们的平均值为0,方差为1。

正则化/归一化(Normalization)

Normalization is the process of scaling individual samples to have unit norm. This process can be useful if you plan to use a quadratic form such as the dot-product or any other kernel to quantify the similarity of any pair of samples. ——scikit-learn.org

  正则化针对的是每行,或者说每个样本的不同特征。一般计算样本之间距离时使用其做归一化处理,比如聚类,K近邻、文本分类。

  正则化的过程是将每个样本缩放到单位范数(每个样本的范数为1),如果后面要使用如二次型(点积)或者其它核方法计算两个样本之间的相似性这个方法会很有用。
  Normalization主要思想是对每个样本计算其p-范数,然后对该样本中每个元素除以该范数,这样处理的结果是使得每个处理后样本的p-范数(L1,L2)等于1。

p-范数的计算公式: ∣ ∣ X ∣ ∣ p = ( ∣ x 1 ∣ p + ∣ x 2 ∣ p + . . . + ∣ x n ∣ p ) 1 p ||X||_p=( |x_1|^p + |x_2|^p + ... + |x_n|^p )^{\frac{1}{p}} Xp=(x1p+x2p+...+xnp)p1
公式详请见文末补充图片

该方法主要应用于文本分类和聚类中。例如,对于两个TF-IDF向量的l2-norm进行点积,就可以得到这两个向量的余弦相似性。

sklern应用(scikit-learn 0.18.2)

1. 划分数据(train_test_split)

**数据划分:**一般使用随机划分,训练集:测试集 = 7 : 3。

 实际项目中,如果数据由不同时间下产生。后产生的数据将一定程度的包含之前的“数据特点”。随机划分后,在后面的分析中,容易造成过拟合,但后续测试集测试结果依然会很好!
 所以为了防止这种情况的发生,实际项目中一般将时间靠前的数据做训练集,时间靠后的数据作为测试集。

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.7) #train 70%, test 30%

2. z-score(scale/StandardScaler)

有两种方式
1、**sklearn.preprocessing.scale()**函数:直接将给定数据进行标准化;
2、sklearn.preprocessing.StandardScaler类:可以保存训练集中的参数(均值、方差)直接使用其对象转换测试集数据。由于提供了fit/transform的API接口,所以实际操作中常用此方式。

  对训练集进行处理(fit),计算出平均数与标准差,将结果应用(transform)于训练集与测试集

from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
ss.fit(X_train)
X_train_std = ss.transform(X_train)
X_test_std = ss.transform(X_test)

3. 属性范围的缩放(MinMaxScaler)

 上述方法将属性设置在0附近,有时需要将其设置在指定的范围内(通常0~1)。

目的:
1、对于方差非常小的属性可以增强其稳定性;
2、维持稀疏矩阵中为0的条目。

from sklearn.preprocessing import MinMaxScaler
mms = MinMaxScaler((0,2))
mms.fit(X_train)
X_train_mms = mms.transform(X_train)
X_test_mms = mms.transform(X_test)

4. Normalization

与标准化相似,同样包含两种方式
1、sklearn.preprocessing.normalize():函数;
2、sklearn.preprocessing.Normalizer():类,提供了fit/transform接口,但接口并无意义,因为正则化是样本之间的应用,样本之间一般可认为无关联。虽无意义,但实际项目中的pipeline中需要统一接口,所以也常用此方式。

from sklearn.preprocessing import Normalizer
norm = Normalizer(norm = l2)		# norm = L2
norm.fit(X_train)                   # fit does nothing
norm.transform(X_train)
norm.transform(X_test)              # 虽无意义,也不会报错,但可以这么使用

补充图片

图片来自
http://www.cnblogs.com/chaosimple/p/4153167.html
https://en.wikipedia.org/wiki/Norm_(mathematics)

[Sklearn应用] Preprocessing data(一) 标准化(Standardization) 与 正则化/归一化(Normalization)_第1张图片

[Sklearn应用] Preprocessing data(一) 标准化(Standardization) 与 正则化/归一化(Normalization)_第2张图片

你可能感兴趣的:(机器学习)