几种数据标准化的方法
数据的标准化(standardization)是将数据按比例缩放,使之落入一个小的特定区间。在某些比较和评价的指标处理中经常会用到,去除数据的单位限制,将其转化为无量纲的纯数值,便于不同单位或量级的指标能够进行比较和加权。其中最典型的就是数据的归一化(normalization)处理,即将数据统一映射到[0,1]区间上,常见的数据归一化的方法有:
(1)最大最小标准化(Min-Max Normalization)
这个过程使得特征的范围在[0,1]内。首先计算每个数值特征的最小值个最大值,然后对特征的每个值均进行以下变换 :
def MaxMinNormalization(x):
"""[0,1] normaliaztion"""
x = (x - np.min(x)) / (np.max(x) - np.min(x))
return x
import numpy as np
def MaxMinNormalization(x):
"""[0,1] normaliaztion"""
x = (x - np.min(x)) / (np.max(x) - np.min(x))
return x
height=[188,176,170,165,168,174]
height_new=MaxMinNormalization(height)
print(height_new)
# -*- coding: utf-8 -*-
"""
@Time : 2022/10/3 23:34
@Author : Xu Yong Kang
"""
from sklearn import preprocessing
import pandas
data={'height':[188,176,170,165,168,174]}#用字典来存放数据
price_frame=pandas.DataFrame(data)#把字典类型转化为dataframe对象
min_max_normalizer=preprocessing.MinMaxScaler(feature_range=(0,1))
#feature_range设置最大最小变换值,默认(0,1)
scaled_data=min_max_normalizer.fit_transform(price_frame)
#将数据缩放到设置固定区间
price_frame_normalized=pandas.DataFrame(scaled_data)
#将变换后的数据转换为dataframe对象
print(price_frame_normalized)
结果:
0 1.000000
1 0.478261
2 0.217391
3 0.000000
4 0.130435
5 0.391304
(2)Z-分数标准化(Z-Score Normalization):当数据包含离群值时,最大最小标准化并不是首选。在存在离群值的情况下,随着数据范围的增加,数值将不断接近零。Z-Score常用于标准化技术。Z-Score遵循统计学原理,使数据平均值为0,标准差为1。
这种标准化我在第一篇文章时间序列的预处理中使用过

def ZscoreNormalization(x):
"""Z-score normaliaztion"""
x = (x - np.mean(x)) / np.std(x)
return x
height=[188,176,170,165,168,174]
height_new=ZscoreNormalization(height)
print(height_new)
from sklearn import preprocessing
import pandas
data={'height':[188,176,170,165,168,174]}#用字典来存放数据
price_frame=pandas.DataFrame(data)#把字典类型转化为dataframe对象
normalizer=preprocessing.scale(price_frame)
#沿着某个轴标准化数据集,以均值为中心,以分量为单位方差
price_frame_normalized=pandas.DataFrame(normalizer,columns=['price'])
#将标准化的数据转换为dataframe对象,将列名改为price
print(price_frame_normalized)
结果:
[ 1.9507511 0.3363364 -0.47087096 -1.14354375 -0.73994007 0.06726728]
而在matlab中,我们可以直接利用zscore(x)这个函数来将数据标准化。
当x是一个向量时,采用z方法得到的仍然是一个向量:
>> a=[1,2,3]
a =
1 2 3
>> b=zscore(a)
b =
-1 0 1
>>
当X是一个矩阵时,采用zscore方法仍然是一个矩阵,在计算的过程中使用的均值及标准差使用的是每一列的均值与方差:
>> a=[1,2,3;4,5,6]
a =
1 2 3
4 5 6
>> b=zscore(a)
b =
-0.7071 -0.7071 -0.7071
0.7071 0.7071 0.7071
(3)均值归一化:通过原始数据的均值、最大值和最小值来进行数据的标准化。

(4)指数转换:通过对原始数据的值进行相应的指数变换来进行数据的标准化。进行指数转换常见的函数方法有lg函数、SoftMax函数和Sigmoid函数。


import numpy as np
height=[188,176,170,165,168,174]
z = np.array(height)
height_new= np.exp(z)/sum(np.exp(z))
print(height_new)
结果:
[9.99993007e-01 6.14416939e-06 1.52298732e-08 1.02618079e-10
2.06113921e-09 8.31522904e-07]
(5)小数定标(Decimal scaling)标准化:通过移动小数点的位置来进行数据的标准化。

式中,x为原始数据,j表示最大绝对值的位数。
(6)向量归一化:通过用原始数据中的每个值除以所有数据之和来进行数据的标准化。

式中,x为原始数据,分母为所有数据之和。
六种方法Python实现如下:
# -*- coding: utf-8 -*-
"""
@Time : 2022/10/3 23:40
@Author : Xu Yong Kang
"""
import numpy as np
import math
class Standardization:
def __init__(self):
self.height = [188,176,170,165,168,174]
self.x_max = max(self.height) # 最大值
self.x_min = min(self.height) # 最小值
self.x_mean = sum(self.height) / len(self.height) # 平均值
self.x_std = np.std(self.height) # 标准差
print("原始数据:\n{}".format(self.height))
def Min_Max(self):
height_new = list()
distance = self.x_max - self.x_min
for x in self.height:
height_new.append(round((x - self.x_min) / distance, 3)) # 保留3位小数
print("Min_Max标准化结果:\n{}".format(height_new))
def Z_Score(self):
height_new= list()
for x in self.height:
height_new.append(round((x - self.x_mean) / self.x_std, 4))
print("Z_Score标准化结果:\n{}".format(height_new))
def DecimalScaling(self):
height_new= list()
j = self.x_max // 10 if self.x_max % 10 == 0 else self.x_max // 10 + 1
for x in self.height:
height_new.append(round(x / (math.pow(10, j)), 4)) # 保留4位小数
print("DecimalScaling标准化结果:\n{}".format(height_new))
def Mean(self):
height_new = list()
distance = self.x_max - self.x_min
for x in self.height:
height_new.append(round((x - self.x_mean) / distance, 4)) # 保留4位小数
print("Mean标准化结果:\n{}".format(height_new))
def Vector(self):
height_new= list()
height_sum = sum(self.height)
for x in self.height:
height_new.append(round(x / height_sum, 4)) # 保留4位小数
print("Vector标准化结果:\n{}".format(height_new))
def exponential(self):
height_new_1= list() # lg
height_new_2= list() # SoftMax
height_new_3= list() # Sigmoid
sum_e = sum([math.exp(x) for x in self.height])
for x in self.height:
height_new_1.append(round(math.log10(x) / math.log10(self.x_max), 4)) # 保留4位小数
height_new_2.append(round(math.exp(x) / sum_e, 4)) # 保留4位小数
height_new_3.append(round(1 / (1 + math.exp(-x)), 4)) # 保留4位小数
print("lg标准化结果:\n{}".format(height_new_1))
print("SoftMax标准化结果:\n{}".format(height_new_2))
print("Sigmod标准化结果:\n{}".format(height_new_3))
def xyk(self):
XYK.Min_Max()
XYK.Z_Score()
XYK.DecimalScaling()
XYK.Mean()
XYK.Vector()
XYK.exponential()
if __name__ == '__main__':
XYK = Standardization()
XYK.xyk()
结果:
原始数据:
[188, 176, 170, 165, 168, 174]
Min_Max标准化结果:
[1.0, 0.478, 0.217, 0.0, 0.13, 0.391]
Z_Score标准化结果:
[1.9508, 0.3363, -0.4709, -1.1435, -0.7399, 0.0673]
DecimalScaling标准化结果:
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Mean标准化结果:
[0.6304, 0.1087, -0.1522, -0.3696, -0.2391, 0.0217]
Vector标准化结果:
[0.1806, 0.1691, 0.1633, 0.1585, 0.1614, 0.1671]
lg标准化结果:
[1.0, 0.9874, 0.9808, 0.9751, 0.9785, 0.9852]
SoftMax标准化结果:
[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Sigmod标准化结果:
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)