标签:
参考:http://scikit-learn.org/stable/modules/preprocessing.html
主要讲述The sklearn.preprocessing package的utility
function and transformer classes,包括standardization、normalization、binarization、encoding
categorical features、process missing value。
1、Standardization, or mean removal and variance scaling(标准化:去均值、除方差)
所谓standardization(标准化),就是指features处于standard normally distribut(高斯分布:均值是0、方差是1),强调的是,所有的features都是标准化的,防止某个features权重过大影响estimators的结果。
(详细内容之前,看一下Further discussion on the importance of centering and scaling data:http://www.faqs.org/faqs/ai-faq/neural-nets/part2/section-16.html)
实际操作中,我们不关心分布的具体形状,只需要每个features减去本features的均值,然后除以本features的标准差。
主要介绍scale function、StandardScaler class和MinMaxScaler:
The function scale provides a quick and easy way to perform this operation on a single array-like dataset:
Scaled data has zero mean and unit variance:
The preprocessing module further provides a utility class StandardScaler ,可以计算训练集的均值和方差,然后将相同的transformation应用于测试集. This class is hence suitable for use in the early steps of a sklearn.pipeline.Pipeline:
The scaler instance can then be used on new data to transform it the same way it did on the training set:
It is possible to disable either centering or scaling by either passing with_mean=False or with_std=False to the constructor ofStandardScaler.
MinMaxScaler看名字就知道,将特征值缩放到min和max之间,常见的是缩放到0和1之间。好处是,不仅能够保持sparse data中得0仍然是0,还可以增加处理小方差特征的鲁棒性。
实现细节如下:
(其实看出来了,实现时操作的是1d array,而不是2d的X,所以对于regression任务,可以考虑缩放target variables)
如果不直接对训练集使用fit_transform,而是使用fit ,那么相同的transformation过程同样可以应用到测试集上:
(除了center、scale操作,有些model还假设特征之间的linear independence,如PCA处理图像时,关于移除特征之间的linear correlation,参考: sklearn.decomposition.PCA or sklearn.decomposition.RandomizedPCA with whiten=True)
未完待续。。。。
版权声明:本文为博主原创文章,未经博主允许不得转载。
scikit-learn:4.3. Preprocessing data(standardi/normali/binari..zation、encoding、missing value)
标签:
原文地址:http://blog.csdn.net/mmc2015/article/details/47016313