reference: http://www.cnblogs.com/chaosimple/p/4153167.html

一、标准化(Z-Score),或者去除均值和方差缩放

公式为:(X-mean)/std  计算时对每个属性/每列分别进行。

将数据按期属性(按列进行)减去其均值,并处以其方差。得到的结果是,对于每个属性/每列来说所有数据都聚集在0附近,方差为1。

实现时,有两种不同的方式:

  • 使用sklearn.preprocessing.scale()函数,可以直接将给定数据进行标准化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> from sklearn import preprocessing
>>> import numpy as np
>>> X = np.array([[ 1., -1.,  2.],
...               [ 2.,  0.,  0.],
...               [ 0.,  1., -1.]])
>>> X_scaled = preprocessing.scale(X)
 
>>> X_scaled                                         
array([[ 0.  ..., -1.22...,  1.33...],
       [ 1.22...,  0.  ..., -0.26...],
       [-1.22...,  1.22..., -1.06...]])
 
>>>#处理后数据的均值和方差
>>> X_scaled.mean(axis=0)
array([ 0.,  0.,  0.])
 
>>> X_scaled.std(axis=0)
array([ 1.,  1.,  1.])
  • 使用sklearn.preprocessing.StandardScaler类,使用该类的好处在于可以保存训练集中的参数(均值、方差)直接使用其对象转换测试集数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> scaler = preprocessing.StandardScaler().fit(X)
>>> scaler
StandardScaler(copy=True, with_mean=True, with_std=True)
 
>>> scaler.mean_                                     
array([ 1. ...,  0. ...,  0.33...])
 
>>> scaler.std_                                      
array([ 0.81...,  0.81...,  1.24...])
 
>>> scaler.transform(X)                              
array([[ 0.  ..., -1.22...,  1.33...],
       [ 1.22...,  0.  ..., -0.26...],
       [-1.22...,  1.22..., -1.06...]])
 
 
>>>#可以直接使用训练集对测试集数据进行转换
>>> scaler.transform([[-1.1., 0.]])               
array([[-2.44...,  1.22..., -0.26...]])

二、将属性缩放到一个指定范围

除了上述介绍的方法之外,另一种常用的方法是将属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现。

使用这种方法的目的包括:

1、对于方差非常小的属性可以增强其稳定性。

2、维持稀疏矩阵中为0的条目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> X_train = np.array([[ 1., -1.2.],
...                     [ 2.0.0.],
...                     [ 0.1., -1.]])
...
>>> min_max_scaler = preprocessing.MinMaxScaler()
>>> X_train_minmax = min_max_scaler.fit_transform(X_train)
>>> X_train_minmax
array([[ 0.5       0.        1.        ],
       [ 1.        0.5       0.33333333],
       [ 0.        1.        0.        ]])
 
>>> #将相同的缩放应用到测试集数据中
>>> X_test = np.array([[ -3., -1.4.]])
>>> X_test_minmax = min_max_scaler.transform(X_test)
>>> X_test_minmax
array([[-1.5       0.        1.66666667]])
 
 
>>> #缩放因子等属性
>>> min_max_scaler.scale_                            
array([ 0.5       0.5       0.33...])
 
>>> min_max_scaler.min_                              
array([ 0.        0.5       0.33...])

当然,在构造类对象的时候也可以直接指定最大最小值的范围:feature_range=(min, max),此时应用的公式变为:

X_std=(X-X.min(axis=0))/(X.max(axis=0)-X.min(axis=0))

X_scaled=X_std/(max-min)+min

三、正则化(Normalization)

正则化的过程是将每个样本缩放到单位范数(每个样本的范数为1),如果后面要使用如二次型(点积)或者其它核方法计算两个样本之间的相似性这个方法会很有用。

Normalization主要思想是对每个样本计算其p-范数,然后对该样本中每个元素除以该范数,这样处理的结果是使得每个处理后样本的p-范数(l1-norm,l2-norm)等于1。

             p-范数的计算公式:||X||p=(|x1|^p+|x2|^p+...+|xn|^p)^1/p

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

1、可以使用preprocessing.normalize()函数对指定数据进行转换:

1
2
3
4
5
6
7
8
9
>>> X = [[ 1., -1.2.],
...      [ 2.0.0.],
...      [ 0.1., -1.]]
>>> X_normalized = preprocessing.normalize(X, norm='l2')
 
>>> X_normalized                                     
array([[ 0.40..., -0.40...,  0.81...],
       [ 1.  ...,  0.  ...,  0.  ...],
       [ 0.  ...,  0.70..., -0.70...]])

2、可以使用processing.Normalizer()类实现对训练集和测试集的拟合和转换:

1
2
3
4
5
6
7
8
9
10
11
12
>>> normalizer = preprocessing.Normalizer().fit(X)  # fit does nothing
>>> normalizer
Normalizer(copy=True, norm='l2')
 
>>>
>>> normalizer.transform(X)                           
array([[ 0.40..., -0.40...,  0.81...],
       [ 1.  ...,  0.  ...,  0.  ...],
       [ 0.  ...,  0.70..., -0.70...]])
 
>>> normalizer.transform([[-1.1., 0.]])            
array([[-0.70...,  0.70...,  0.  ...]])

补充:

[Scikit-Learn] - 数据预处理 - 归一化/标准化/正则化的更多相关文章

  1. 关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化

    一.标准化(Z-Score),或者去除均值和方差缩放 公式为:(X-mean)/std  计算时对每个属性/每列分别进行. 将数据按期属性(按列进行)减去其均值,并处以其方差.得到的结果是,对于每个属 ...

  2. 【原】关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化

    一.标准化(Z-Score),或者去除均值和方差缩放 公式为:(X-mean)/std  计算时对每个属性/每列分别进行. 将数据按期属性(按列进行)减去其均值,并处以其方差.得到的结果是,对于每个属 ...

  3. 使用sklearn进行数据预处理 —— 归一化/标准化/正则化

    一.标准化(Z-Score),或者去除均值和方差缩放 公式为:(X-mean)/std  计算时对每个属性/每列分别进行. 将数据按期属性(按列进行)减去其均值,并除以其方差.得到的结果是,对于每个属 ...

  4. Python数据预处理—归一化,标准化,正则化

    关于数据预处理的几个概念 归一化 (Normalization): 属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现. 常用的 ...

  5. 数据预处理:标准化(Standardization)

    注:本文是人工智能研究网的学习笔记 常用的数据预处理方式 Standardization, or mean removal and variance scaling Normalization: sc ...

  6. sklearn中的数据预处理----good!! 标准化 归一化 在何时使用

    RESCALING attribute data to values to scale the range in [0, 1] or [−1, 1] is useful for the optimiz ...

  7. 数据的特征预处理?(归一化)&(标准化)&(缺失值)

    特征处理是什么: 通过特定的统计方法(数学方法)将数据转化成为算法要求的数据 sklearn特征处理API: sklearn.preprocessing 代码示例:  文末! 归一化: 公式:    ...

  8. Matlab中的数据预处理-归一化(mapminmax)与标准化(mapstd)

    一.mapminmax 意思是将矩阵的每一行处理成[-1,1]区间,此时对于模式识别或者其他统计学来说,数据应该是每一列是一个样本,每一行是多个样本的同一维,即对于一个M*N的矩阵来说,样本的维度是M ...

  9. spark 数据预处理 特征标准化 归一化模块

    #We will also standardise our data as we have done so far when performing distance-based clustering. ...

随机推荐

  1. [React Intl] Install and Configure the Entry Point of react-intl

    We’ll install react-intl, then add it to the mounting point of our React app. Then, we’ll use react- ...

  2. Dynamics CRM 2015/2016 Web API:Unbound Function 和 Bound Function

    今天我们来看看Dynamics CRM Web API Function 吧, 这是一个新概念,刚接触的时候我也是比較的迷糊.这种命名确实是和之前的那套基于SOAP协议的API全然联系不上.好了,不说 ...

  3. 动态规划求解序列问题(LIS、JLIS)

    1. 最长递增子序列 不要求位置连续:要求大小严格递增(strictly increasing) 穷举法解题 首先以每个数字为单位分割寻找最长递增子序列: int lis(const vector&l ...

  4. POJ 3259 Wormholes 邻接表的SPFA判断负权回路

    http://poj.org/problem?id=3259 题目大意: 一个农民有农场,上面有一些虫洞和路,走虫洞可以回到 T秒前,而路就和平常的一样啦,需要花费时间走过.问该农民可不可能从某个点出 ...

  5. 10.10 android输入系统_APP获得并处理输入事件流程

    APP对fd/InputChannel的注册过程: new WindowInputEventReceiver extends InputEventReceiver//InputEventReceive ...

  6. matplotlib 可视化 —— style sheets

    Customizing plots with style sheets Matplotlib Style Gallery 1. 常见 style ggplot: bmh:Bayesian Method ...

  7. 【BZOJ 3156】防御准备

    [链接] 链接 [题意] 在这里输入题意 [题解] 把a倒过来 设f[i]表示在i放一个防御塔的最小花费; 我们如果从j转移过来 就表示j+1..i-1这一段放人偶. s[i] = 1 + 2 + . ...

  8. [React] Use React.cloneElement to Modify and Add Additional Properties to React Children

    In this lesson we'll show how to use React.cloneElement to add additional properties to the children ...

  9. Longest Increasing Subsequences(最长递增子序列)的两种DP实现

    一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn).     二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...

  10. 结构体什么时候用.什么时候用->