import math
import os
from zhuguan import zhu_jiegou
import pandas as pd
import numpy as np
def zhengxiang(k0):
return (k0-np.min(k0))/(np.max(k0)-np.min(k0))
def fanxiang(k0):
return (np.max(k0)-k0)/(np.max(k0)-np.min(k0))
def jiankangdu(zhu_excel_path,ke_excel_path):
ke_excel = pd.read_excel(ke_excel_path)
ke_array = np.array(ke_excel.iloc[:,1:])
print(ke_excel,ke_array)
k0 =np.expand_dims(zhengxiang(ke_array[0,:]),axis=0)
k1 =np.expand_dims(zhengxiang(ke_array[1,:]),axis=0)
k2 =np.expand_dims(zhengxiang(ke_array[2,:]),axis=0)
k3 =np.expand_dims(fanxiang(ke_array[3,:]),axis=0)
k4 =np.expand_dims(zhengxiang(ke_array[4,:]),axis=0)
print(k0)
print(k1)
print(k2)
print(k3)
print(k4)
#标准化结果
result_array = np.concatenate((k0,k1,k2,k3,k4),axis=0)
print(result_array)
Y = np.max(result_array,axis=1)
print(Y)
chazhi = np.tile(np.expand_dims(Y,axis=1),5)-result_array
print(chazhi)
chazhi_min = np.min(chazhi,axis=0)
print(chazhi_min)
chazhi_max = np.max(chazhi,axis=0)
print(chazhi_max)
chazhi_min_min = np.min(chazhi_min)
chazhi_max_max = np.max(chazhi_max)
print(chazhi_min_min,chazhi_max_max)
#关联函数
guanlian_array = (chazhi_min_min+0.5*chazhi_max_max)/(chazhi+0.5*chazhi_max_max)
print(guanlian_array)
#熵值
fij = guanlian_array/np.tile(np.expand_dims(np.sum(guanlian_array,axis=1),axis=1),5)
print(fij)
yj = -math.pow(math.log(5),-1)*np.sum(fij*np.log(fij),axis=1)
print(yj)
ke_w = (1-yj)/np.sum(1-yj)
print(ke_w)
#计算组合权重
zhu_w = zhu_jiegou(zhu_excel_path)
print(zhu_w)
wj = zhu_w*ke_w/np.sum(zhu_w*ke_w)
print(wj)
pwv = np.tile(np.expand_dims(wj,axis=1),5)*guanlian_array/np.tile(np.expand_dims(np.sum(np.tile(np.expand_dims(wj,axis=1),5)*guanlian_array,axis=1),axis=1),5)
print(pwv)
jkd = -np.sum(pwv*np.log(pwv),axis=0)
# print(quanzhong)
return jkd
#=======================================================================================================================
if __name__ == '__main__':
zhu_excel_path = r'C:\Users\sjzx_deeplearning_01\Desktop\hg\data1.xls'
ke_excel_path = r'C:\Users\sjzx_deeplearning_01\Desktop\hg\data2.xls'
save_excel_path = r'C:\Users\sjzx_deeplearning_01\Desktop\result.xls'
jiankangdu_value = jiankangdu(zhu_excel_path,ke_excel_path)
print('健康度:',jiankangdu_value)
if os.path.exists(save_excel_path):
os.remove(save_excel_path)
writer = pd.ExcelWriter(save_excel_path)
array_1 = np.array(jiankangdu_value)
data_1 = pd.DataFrame(array_1)
# header参数表示列的名称,index表示行的标签
data_1.to_excel(writer, 'sheet_1', float_format='%.8f', header=False, index=False) # 不要行和列的标签
# 注意写多个的时候要把所有数据都给了writer后再调用save函数,要不然可能只有一个sheet
writer.save()
writer.close()