機器學習之數據預處理——降噪
1.降噪方法
html
money=[800,1000,1200,1500,1600,1800,2000,2300,\ 2500,2800,3000,3500,4000,4500,4800,5000] cut1=pd.cut(pd.Series(money), bins=[0,1000,2000,3000,4000,5000])#設定分箱區間 #0能夠寫,也能夠不寫 print(pd.value_counts(cut1))
cut3=pd.qcut(pd.Series(money), 4)#設定分箱數,每組數據量相同 print(pd.value_counts(cut3))
2.分箱平滑
python
#平滑噪聲—等深分箱—均值平滑 import pandas as pd import numpy as np def aequilatus_box_mean(data,bins): length=data.shape[0] labels=[] for i in range(bins): labels.append('a'+str(i+1))#添加標籤 new_data=pd.qcut(data.iloc[:,0],bins,labels=labels)#等深分箱 data['label']=new_data for label in labels: label_index_min=data[data.label==label].index.min()#分箱後索引最小值 label_index_max=data[data.label==label].index.max()#分箱後索引最大值 data.loc[label_index_min:label_index_max,data.columns[0]]=np.mean( data.A[label_index_min:label_index_max+1,])#根據label及索引,修改A爲各箱均值 return data if __name__=="__main__": data=pd.DataFrame({'A':[11,13,15,20,20,23,26,29,35]}) bins=3 print("均值平滑") print(aequilatus_box_mean(data,3))
#平滑噪聲—等深分箱—中值平滑 import pandas as pd import numpy as np def aequilatus_box_median(data,bins): length=data.shape[0] labels=[] for i in range(bins): labels.append('a'+str(i+1)) new_data=pd.qcut(data.A,bins,labels=labels)#等深分箱 data['label']=new_data for label in labels: label_index_min=data[data.label==label].index.min()#分箱後索引最小值 label_index_max=data[data.label==label].index.max()#分箱後索引最大值 data.loc[label_index_min:label_index_max,'A']=np.median( data.A[label_index_min:label_index_max+1,])#根據label及索引,修改A爲各箱均值 return data if __name__=="__main__": data=pd.DataFrame({'A':[11,13,15,20,20,23,26,29,35]}) bins=3 print("中值平滑") print(aequilatus_box_median(data,3))
#平滑噪聲—等深分箱—邊界平滑 import pandas as pd import numpy as np def aequilatus_box_border(data,bins): length=data.shape[0] labels=[] for i in range(bins): labels.append('a'+str(i+1)) new_data=pd.qcut(data.A,bins,labels=labels)#等深分箱 data['label']=new_data for label in labels: label_index_min=data[data.label==label].index.min() label_index_max=data[data.label==label].index.max() data_min=np.min(data.A[label_index_min:label_index_max+1,]) data_max=np.max(data.A[label_index_min:label_index_max+1,]) for i in range(label_index_min,label_index_max): if(data.loc[i,'A']==data_min or data.loc[i,'A']==data_max): data.loc[i,'A']=data.loc[i,'A'] elif(np.abs(data.loc[i,'A']-data_min)<=np.abs(data.loc[i,'A']-data_max)): data.loc[i,'A']=data_min else: data.loc[i,'A']=data_max return data if __name__=="__main__": data=pd.DataFrame({'A':[11,12,15,21,20,23,26,29,35]}) bins=3 print("邊界平滑") print(aequilatus_box_border(data,3))
編寫打磨課件不易,走過路過別忘記給咱點個贊,小女子在此(❁´ω`❁)謝過!如需轉載,請註明出處,Thanks♪(・ω・)ノapp
參考文獻:
1.https://blog.csdn.net/weixin_40192436/article/details/86706231機器學習
2.https://www.cnblogs.com/serena45/p/5559122.html學習
3.https://www.jianshu.com/p/389682aa5429ui
最後:
對Python感興趣的能夠和我一塊兒交流:點擊連接加入羣聊【python交流羣】:spa