数据预处理——中心化、归一化与标准化

参考: 归一化与标准化

中心化(mean normalization)、归一化(normalization)和标准化(standardization)对原始数据进行处理,有利于后续的训练与分析工作。

中心化处理后的数据均值为 0:

$$ x’ = x-\mu $$

归一化将数据映射到 [0, 1] 之间:

$$ x’ = \frac{x-x{min}}{x{max}-x_{min}} $$

标准化处理后的数据均值为 0,方差为 1:

$$ x’ = \frac{x-\mu}{\sigma} $$

以上 $\mu, \sigma$ 分别为原始数据的均值与方差。

对于标准化和归一化,sklearn 提供了函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
from sklearn import preprocessing
x = np.array([[ 1., -1., 2.],[ 2., 0., 0.],[ 0., 1., -1.]])

# 两个中心化处理调用
mean_x = x - x.mean(axis=0)
mean_x = preprocessing.StandardScaler(with_std=False).fit_transform(x)

# 两个标准化处理调用
std_x = preprocessing.scale(x)
std_x = preprocessing.StandardScaler().fit_transform(x)

# 归一化处理
norm_x = preprocessing.MinMaxScaler().fit_transform(x)