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. BZOJ 3038 上帝造题的七分钟2 树状数组+并查集

    题目大意:一个序列,有两种操作.1.将一段数中的每个数开根号.2.查询一段数的和. 思路:和3211是一个题,有兴趣的能够看看我的那篇博客. CODE: #include <cmath> ...

  2. Leetcode 第 2 题(Add Two Numbers)

    Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...

  3. 总览:SpringCloud基础结构

    客户端: 1.连接 配置中心2.连接 服务中心3.连接 链路跟踪3.feign调用4.熔断机制5.连接 熔断监控6.swagger2 生成的RESTful-API7.消息总线-rabbitMQ实现 基 ...

  4. 关于stm32的输入输出

    https://blog.csdn.net/u011556018/article/details/72629082

  5. 最正经的php post get

    https://www.cnblogs.com/ps-blog/p/6732448.html /** * 模拟post进行url请求 * @param string $url * @param str ...

  6. Your algorithm's runtime complexity must be in the order of O(log n).

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  7. Spinlock implementation in ARM architecture

    Spinlock implementation in ARM architecture   SEV and WFE are the main instructions used for impleme ...

  8. Redis主从高可用缓存

    nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存   一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集 ...

  9. 【35.20%】【CF 706D】Vasiliy's Multiset

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  10. D3D 点列练习

    画四个点在窗体. #pragma once #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib& ...