python 标准化_数据标准化
常見的數(shù)據(jù)標(biāo)準(zhǔn)化方法有以下6種:
1、Min-Max標(biāo)準(zhǔn)化
Min-Max標(biāo)準(zhǔn)化是指對原始數(shù)據(jù)進(jìn)行線性變換,將值映射到[0,1]之間
2、Z-Score標(biāo)準(zhǔn)化
Z-Score(也叫Standard Score,標(biāo)準(zhǔn)分?jǐn)?shù))標(biāo)準(zhǔn)化是指:基于原始數(shù)據(jù)的均值(mean)和標(biāo)準(zhǔn)差(standard deviation)來進(jìn)行數(shù)據(jù)的標(biāo)準(zhǔn)化。
3、小數(shù)定標(biāo)(Decimal scaling)標(biāo)準(zhǔn)化
小數(shù)定標(biāo)標(biāo)準(zhǔn)化是指:通過移動小數(shù)點的位置來進(jìn)行數(shù)據(jù)的標(biāo)準(zhǔn)化。小數(shù)點移動的位數(shù)取決于原始數(shù)據(jù)中的最大絕對值。
4、均值歸一化法
均值歸一化是指:通過原始數(shù)據(jù)中的均值、最大值和最小值來進(jìn)行數(shù)據(jù)的標(biāo)準(zhǔn)化
5、向量歸一化
向量歸一化是指:通過用原始數(shù)據(jù)中的每個值除以所有數(shù)據(jù)之和來進(jìn)行數(shù)據(jù)的標(biāo)準(zhǔn)化
6、指數(shù)轉(zhuǎn)換
指數(shù)轉(zhuǎn)換是指:通過對原始數(shù)據(jù)的值進(jìn)行相應(yīng)的指數(shù)函數(shù)變換來進(jìn)行數(shù)據(jù)的標(biāo)準(zhǔn)化。進(jìn)行指數(shù)轉(zhuǎn)換常見的函數(shù)方法有l(wèi)g函數(shù)、Softmax函數(shù)和Sigmoid函數(shù)
實例1:實現(xiàn)數(shù)據(jù)的標(biāo)準(zhǔn)化
要對原始數(shù)據(jù)[1,2,3,4,5,6,7,8,9]進(jìn)行標(biāo)準(zhǔn)化,代碼如下:
"""
Author: Thinkgamer
Desc:
代碼4-1 Python實現(xiàn)標(biāo)準(zhǔn)化方法
"""
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) # 標(biāo)準(zhǔn)差
def Min_Max(self):
arr_ = list()
for x in self.arr:
# round(x,4) 對x保留4位小數(shù)
arr_.append(round((x - self.x_min) / (self.x_max - self.x_min), 4))
print("經(jīng)過Min_Max標(biāo)準(zhǔn)化后的數(shù)據(jù)為:\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("經(jīng)過Z_Score標(biāo)準(zhǔn)化后的數(shù)據(jù)為:\n{}".format(arr_))
# 有點問題,改為如下這樣
# def DecimalScaling(self):
# arr_ = list()
# j = self.x_max // 10 if self.x_max % 10 == 0 else self.x_max // 10 + 1
# for x in self.arr:
# arr_.append(round(x / math.pow(10, j), 4))
# print("經(jīng)過Decimal Scaling標(biāo)準(zhǔn)化后的數(shù)據(jù)為:\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("經(jīng)過Decimal Scaling標(biāo)準(zhǔn)化后的數(shù)據(jù)為:\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("經(jīng)過均值標(biāo)準(zhǔn)化后的數(shù)據(jù)為:\n{}".format(arr_))
def Vector(self):
arr_ = list()
for x in self.arr:
arr_.append(round(x / sum(self.arr), 4))
print("經(jīng)過向量標(biāo)準(zhǔn)化后的數(shù)據(jù)為:\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("經(jīng)過指數(shù)轉(zhuǎn)換法(log10)標(biāo)準(zhǔn)化后的數(shù)據(jù)為;\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("經(jīng)過指數(shù)轉(zhuǎn)換法(SoftMax)標(biāo)準(zhǔn)化后的數(shù)據(jù)為;\n{}".format(arr_2))
arr_3 = list()
for x in self.arr:
arr_3.append(round(1 / (1 + math.exp(-x)), 4))
print("經(jīng)過指數(shù)轉(zhuǎn)換法(Sigmoid)標(biāo)準(zhǔn)化后的數(shù)據(jù)為;\n{}".format(arr_3))
if name == "main":
dn = DataNorm()
dn.Min_Max()
dn.Z_Score()
dn.DecimalScaling()
dn.Mean()
dn.Vector()
dn.exponential()
在實現(xiàn)數(shù)據(jù)標(biāo)準(zhǔn)化中,使用round函數(shù)來進(jìn)行小數(shù)后數(shù)據(jù)位數(shù)的保留,如round(x,4)表示的是保留小數(shù)點后4位小數(shù)
總結(jié)
以上是生活随笔為你收集整理的python 标准化_数据标准化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eval在python中是什么意思_如何
- 下一篇: memcpy函数_如何理解c语言中的回调