data = pd.read_excel('水質(zhì)數(shù)據(jù).xlsx')
print(data.shape) # 數(shù)據(jù)維度
print(data.isnull().sum()) # 返回缺失值數(shù)量
data.head()
數(shù)據(jù)集為某四個水池(各15周)水質(zhì)指標(biāo)數(shù)據(jù),其中總磷、COD(mg/l)、總氮、氨氮為極小型數(shù)據(jù),PH為區(qū)間型數(shù)據(jù)(范圍為[6,9])。
# 原始數(shù)據(jù)正向化
def func_1(x):
return(x.max()-x) # 極小型
def func_2(x,x_best):
M = (abs(x-x_best)).max()
return(1-abs(x-x_best)/M) # 中間性
def func_3(x,a,b):
M = max(a-min(x), max(x)-b)
y = []
for i in x:
if i < a:
y.append(1-(a-i)/M)
elif i > b:
y.append(1-(i-b)/M)
else:
y.append(1)
return(y) # 區(qū)間型
定義三個函數(shù),函數(shù)用于進(jìn)行原始數(shù)據(jù)的正向化處理。
func_1(x):
func_2(x, x_best):
func_3(x, a, b):
如果 則正向化為 如果 則正向化為
?? ? ? ? ? ? ?如果??則正向化為??
data['總磷'] = func_1(data['總磷']) # 極小型
data['總氮'] = func_1(data['總氮']) # 極小型
data['COD(mg/l)'] = func_1(data['COD(mg/l)']) # 極小型
data['氨氮'] = func_1(data['氨氮']) # 極小型
data['PH'] = func_3(data['PH'], 6, 9) # 區(qū)間型
通過調(diào)用特定的正向化函數(shù),對DataFrame中的幾列數(shù)據(jù)進(jìn)行了不同類型的正向化處理,以便用于后續(xù)的數(shù)據(jù)分析或模型建設(shè)。
import numpy as np
# 標(biāo)準(zhǔn)化
def func(data):
tep_x1 = (data.iloc[:,0::]*data.iloc[:,0::]).sum() # 每個元素平方后按列相加
tep_x2 = np.tile(tep_x1,(data.shape[0], 1)) # 將tep_x1平鋪a行
Z = data.iloc[:,::]/((tep_x2)**0.5) # 標(biāo)準(zhǔn)化
return(Z)
data_bzh = func(data)
data_bzh.head()
標(biāo)準(zhǔn)化,即將數(shù)據(jù)縮放到指定范圍(通常是 ),公式為:
其中,??是標(biāo)準(zhǔn)化后的原始,??是原始數(shù)據(jù)框中的元素,??是第??列的最小值,??是第??列的最大值。
# 熵權(quán)法求每個指標(biāo)權(quán)值
P = data_bzh/np.tile(data_bzh.sum(),(data_bzh.shape[0],1)) # 概率矩陣P
E = -1/np.log(data_bzh.shape[0]) * (P*np.log(P)).sum()
D = 1-E
W = D/D.sum() # 熵權(quán)
print(W)
代碼使用熵權(quán)法,通過對標(biāo)準(zhǔn)化數(shù)據(jù),計算每個指標(biāo)的概率矩陣和信息熵,然后通過離散度計算得到每個指標(biāo)的權(quán)值,最終輸出一個權(quán)值向量 ??,用于評估每個指標(biāo)在多準(zhǔn)則決策中的相對重要性。
tep_max = data_bzh.max()
tep_min = data_bzh.min()
D_P = ((((np.tile(tep_max, (data_bzh.shape[0],1))-data_bzh)**2)*np.tile(W, (data_bzh.shape[0],1))).sum(axis = 1))**0.5
D_N = ((((np.tile(tep_min, (data_bzh.shape[0],1))-data_bzh)**2)*np.tile(W, (data_bzh.shape[0],1))).sum(axis = 1))**0.5
S = D_N/(D_P+D_N) # 未歸一化得分
代碼計算了數(shù)據(jù)框 data_bzh
中每行的未歸一化得分 S
,該得分反映了每個方案相對于理想解和負(fù)理想解的距離。具體而言,使用熵權(quán)法中的極大型指標(biāo),計算每行數(shù)據(jù)相對于最大值(理想解)和最小值(負(fù)理想解)的距離,最終得到未歸一化得分。
import matplotlib.pylab as plt
plt.rcParams['font.sans-serif'] = 'SimHei' # 設(shè)置中文顯示
plt.rcParams['axes.unicode_minus'] = False
df = pd.DataFrame(S)
plt.figure(figsize=(8, 3), dpi = 300)
y1 = df.iloc[0:15, :]
y2 = df.iloc[15:30,:]
y3 = df.iloc[30:45,:]
y4 = df.iloc[45:60,:]
X = range(1, 16)
plt.scatter(X, y1, color= 'k', label='水池#1')
plt.scatter(X, y2, color= 'b', label='水池#2')
plt.scatter(X, y3, color= 'y', label='水池#3')
plt.scatter(X, y4, color= 'r', label='水池#4')
plt.legend(loc='upper left', numpoints=1, ncol=1, fontsize=8, bbox_to_anchor=(1, 1))
plt.title('各水池評價')
plt.xlabel('周數(shù)')
plt.ylabel('評價值')
plt.show()
繪制四個水池在不同周數(shù)的評價值散點(diǎn)圖,展示四個水池每周的評價值分布情況,幫助比較它們的性能。
# 輸出每個水池均值評分
print(df.iloc[0:15, :].mean())
print(df.iloc[15:30,:].mean())
print(df.iloc[30:45,:].mean())
print(df.iloc[45:60,:].mean())
本文章轉(zhuǎn)載微信公眾號@Python機(jī)器學(xué)習(xí)AI