数据挖掘-数据标准化 python实现
- 数据标准化
数据是
[1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
Author: Thinkgamer
Desc:
代码4-1 Python实现标准化方法
"""
import numpy as np
import math
class DataNorm:
def __init__(self):
self.arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
self.x_max = max(self.arr)
self.x_min = min(self.arr)
self.x_mean = sum(self.arr) / len(self.arr)
self.x_std = np.std(self.arr)
def Min_Max(self):
arr_ = list()
for x in self.arr:
arr_.append(round((x - self.x_min) / (self.x_max - self.x_min), 4))
print("经过Min_Max标准化后的数据为:\n{}".format(arr_))
def Z_Score(self):
arr_ = list()
for x in self.arr:
arr_.append(round((x - self.x_mean) / self.x_std, 4))
print("经过Z_Score标准化后的数据为:\n{}".format(arr_))
def DecimalScaling(self):
arr_ = list()
j = 1
x_max = max([abs(one) for one in self.arr])
while x_max / 10 >= 1.0:
j += 1
x_max = x_max / 10
for x in self.arr:
arr_.append(round(x / math.pow(10, j), 4))
print("经过Decimal Scaling标准化后的数据为:\n{}".format(arr_))
def Mean(self):
arr_ = list()
for x in self.arr:
arr_.append(round((x - self.x_mean) / (self.x_max - self.x_min), 4))
print("经过均值标准化后的数据为:\n{}".format(arr_))
def Vector(self):
arr_ = list()
for x in self.arr:
arr_.append(round(x / sum(self.arr), 4))
print("经过向量标准化后的数据为:\n{}".format(arr_))
def exponential(self):
arr_1 = list()
for x in self.arr:
arr_1.append(round(math.log10(x) / math.log10(self.x_max), 4))
print("经过指数转换法(log10)标准化后的数据为;\n{}".format(arr_1))
arr_2 = list()
sum_e = sum([math.exp(one) for one in self.arr])
for x in self.arr:
arr_2.append(round(math.exp(x) / sum_e, 4))
print("经过指数转换法(SoftMax)标准化后的数据为;\n{}".format(arr_2))
arr_3 = list()
for x in self.arr:
arr_3.append(round(1 / (1 + math.exp(-x)), 4))
print("经过指数转换法(Sigmoid)标准化后的数据为;\n{}".format(arr_3))
if __name__ == "__main__":
dn = DataNorm()
dn.Min_Max()
dn.Z_Score()
dn.DecimalScaling()
dn.Mean()
dn.Vector()
dn.exponential()
经过Min_Max标准化后的数据为:
[0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]
经过Z_Score标准化后的数据为:
[-1.5492, -1.1619, -0.7746, -0.3873, 0.0, 0.3873, 0.7746, 1.1619, 1.5492]
经过Decimal Scaling标准化后的数据为:
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
经过均值标准化后的数据为:
[-0.5, -0.375, -0.25, -0.125, 0.0, 0.125, 0.25, 0.375, 0.5]
经过向量标准化后的数据为:
[0.0222, 0.0444, 0.0667, 0.0889, 0.1111, 0.1333, 0.1556, 0.1778, 0.2]
经过指数转换法(log10)标准化后的数据为;
[0.0, 0.3155, 0.5, 0.6309, 0.7325, 0.8155, 0.8856, 0.9464, 1.0]
经过指数转换法(SoftMax)标准化后的数据为;
[0.0002, 0.0006, 0.0016, 0.0043, 0.0116, 0.0315, 0.0856, 0.2326, 0.6322]
经过指数转换法(Sigmoid)标准化后的数据为;
[0.7311, 0.8808, 0.9526, 0.982, 0.9933, 0.9975, 0.9991, 0.9997, 0.9999]