标准化数据
1.离差标准化数据(对原始数据的一种线性变换,结果映射到[0,1]区间)
import pandas as pd
import numpy as np
detail=pd.read_csv('../数据分析/detail.csv',encoding='gbk',index_col=0)
def MinMaxScale(data): #自定义离差标准化函数
data=(data-data.min())/(data.max()-data.min())
return data
data1=MinMaxScale(detail['counts'])
data2=MinMaxScale(detail['amounts'])
data3=pd.concat([data1,data2],axis=1)
print('离差标准化之前 销量和售价数据为:\n',detail[['counts','amounts']].head())
print('离差标准化之后 销量和售价数据为:\n',data3.head())
2.标准差标准化数据(经过该方法处理的数据均值为0,标准差为1)
import pandas as pd
import numpy as np
detail=pd.read_csv('../数据分析/detail.csv',encoding='gbk',index_col=0)
def StandardScaler(data): #自定义标准差标准化函数
data=(data-data.mean())/data.std()
return data
data4=StandardScaler(detail['counts'])
data5=StandardScaler(detail['amounts'])
data6=pd.concat([data4,data5],axis=1)
print('标准差标准化之前 销量和售价数据为:\n',detail[['counts','amounts']].head())
print('标准差标准化之前 销量和售价数据为:\n',data6.head())
3.小数定标标准化数据(数据缩小,将数据映射到区间[-1,1] )
import pandas as pd
import numpy as np
detail=pd.read_csv('../数据分析/detail.csv',encoding='gbk',index_col=0)
def DecimalScaler(data):
data=data/10**np.ceil(np.log10(data.abs().max()))
return data
data7=DecimalScaler(detail['counts'])
data8=DecimalScaler(detail['amounts'])
data9=pd.concat([data7,data8],axis=1)
print('小数定标标准化之前 销量和售价数据为:\n',detail[['counts','amounts']].head())
print('小数定标准化之前 销量和售价数据为:\n',data9.head())