概念:
原因:
由于進行分類器或模型的建立與訓練時,輸入的數據范圍可能比較大,同時樣本中各數據可 能量綱不一致,這樣的數據容易對模型訓練或分類器的構建結果產生影響,因此需要對其進行標準 化處理,去除數據的單位限制,將其轉化為無量綱的純數值,便于不同單位或量級的指標能夠進行 比較和加權。
其中最典型的就是數據的歸一化處理,即將數據統一映射到[0,1]區間上。
z-score標準化(零均值歸一化zero-mean normalization):
? 經過處理后的數據均值為0,標準差為1(正態分布)
? 其中μ是樣本的均值, σ是樣本的標準差
代碼實現
import numpy as np
import matplotlib.pyplot as plt
#歸一化的兩種方式
def Normalization1(x):'''歸一化(0~1)''''''x_=(x?x_min)/(x_max?x_min)'''return [(float(i)-min(x))/float(max(x)-min(x)) for i in x]
def Normalization2(x):'''歸一化(-1~1)''''''x_=(x?x_mean)/(x_max?x_min)'''return [(float(i)-np.mean(x))/(max(x)-min(x)) for i in x]
#標準化
def z_score(x):'''x?=(x?μ)/σ'''x_mean=np.mean(x)s2=sum([(i-np.mean(x))*(i-np.mean(x)) for i in x])/len(x)return [(i-x_mean)/s2 for i in x]l=[-10, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 30]
l1=[]
# for i in l:
# i+=2
# l1.append(i)
# print(l1)
cs=[]
for i in l:c=l.count(i)cs.append(c)
print(cs)
n=Normalization2(l)
z=z_score(l)
print(n)
print(z)
'''
藍線為原始數據,橙線為z
'''
plt.plot(l,cs)
plt.plot(z,cs)
plt.show()