m个样本,共n个指标, X i j X_{ij} Xij为为第 i i i个样本的第 j j j个指标的数值, i = 1 , 2 , 3 , . . . m ; j = 1 , 2 , 3... n . i = 1 , 2 , 3 , . . . m ; j = 1 , 2 , 3... n. i=1,2,3,...m;j=1,2,3...n.
各项指标的计量单位以及方向不统一的情况下,需要对对数据进行标准化,为了避免求熵值时对数无意义,可以为每个值加上较小数量级的实数,如0.001
X ′ = X i j − M i n ( X i j ) M a x ( X i j ) − M i n ( X i j ) X'= \frac{X_{ij}- Min(X_{ij})}{Max(X_{ij})-Min(X_{ij})} X′=Max(Xij)−Min(Xij)Xij−Min(Xij)
X ′ = M a x ( X i j ) − X i j M a x ( X i j ) − M i n ( X i j ) X' = \frac{Max(X_{ij})-X_{ij}}{Max(X_{ij})-Min(X_{ij})} X′=Max(Xij)−Min(Xij)Max(Xij)−Xij
计算样本权重:
P i j = X i j ∑ i = 1 n X i j P_{ij} = \frac{X_{ij}}{\sum_{i=1}^nX_{ij}} Pij=∑i=1nXijXij
计算指标熵值:
e j = − 1 l n ( m ) ∗ ∑ i = 1 m ( P i j ∗ l n ( P i j ) ) e_j = -\frac{1}{ln(m)}*\sum_{i=1}^m(P_{ij}*ln(P_{ij})) ej=−ln(m)1∗i=1∑m(Pij∗ln(Pij))
,其中m为样本个数。
某项指标的信息效用值取决于该指标的信息熵与 1 之间的差值,它的值直接影响权重的大小。信息效用值越大,对评价的重要性就越大,权重也就越大。
d j = 1 − e j d_j=1-e_j dj=1−ej
利用熵值法估算各指标的权重,其本质是利用该指标信息的差异系数来计算,其差异系数越高,对评价的重要性就越大(或称权重越大,对评价结果的贡献就越大)
第j jj项指标的权重:
w j = d j ∑ j = 1 m d j w_j=\frac{d_j}{\sum_{j=1}^md_j} wj=∑j=1mdjdj
z i = ∑ j = 1 m w j x i j z_i=\sum_{j=1}^mw_jx_{ij} zi=j=1∑mwjxij
样品编号 | 氨基酸总量 | 天门冬氨酸 | 苏氨酸 | 丝氨酸 | 谷氨酸 | 脯氨酸 |
---|---|---|---|---|---|---|
葡萄样品1 | 2027.96 | 101.22 | 393.42 | 77.61 | 266.6 | 723.88 |
葡萄样品2 | 2128.82 | 64.43 | 140.62 | 71.94 | 39.26 | 1560.97 |
葡萄样品3 | 8397.28 | 108.07 | 222.35 | 173.08 | 67.54 | 7472.28 |
葡萄样品4 | 2144.68 | 79.39 | 133.83 | 158.74 | 156.72 | 1182.23 |
葡萄样品5 | 1844 | 52.28 | 145.09 | 164.05 | 102.43 | 816.08 |
葡萄样品6 | 3434.17 | 68.01 | 102.42 | 75.78 | 80.6 | 2932.76 |
葡萄样品7 | 2391.16 | 65.1 | 267.76 | 239.2 | 208.97 | 1096.28 |
葡萄样品8 | 1950.76 | 72.09 | 345.87 | 44.23 | 176.02 | 962.01 |
import numpy as np
import pandas as pd
## 读取数据
data=pd.read_csv('redputao.csv',encoding='utf-8',index_col=0)
indicator=data.columns.tolist() ## 指标个数 多少列
project=data.index.tolist() ## 样本个数 多少行
value=data.values
print(indicator)
print(project)
print(value)
## 定义数据标准化函数。为了避免求熵值时对数无意义,对数据进行平移,对标准化后的数据统一加了常数0.001
def std_data(value,flag):
for i in range(len(indicator)):
if flag[i]=='+':
value[:,i]=(value[:,i]-np.min(value[:,i],axis=0))/(np.max(value[:,i],axis=0)-np.min(value[:,i],axis=0))+0.01
elif flag[i]=='-':
value[:,i]=(np.max(value[:,i],axis=0)-value[:,i])/(np.max(value[:,i],axis=0)-np.min(value[:,i],axis=0))+0.01
return value
# 定义熵值法函数、熵值法计算变量的权重
def cal_weight(indicator, project, value):
p = np.array([[0.0 for i in range(len(indicator))] for i in range(len(project))])
# print(p)
for i in range(len(indicator)):
p[:, i] = value[:, i] / np.sum(value[:, i], axis=0)
e = -1 / np.log(len(project)) * sum(p * np.log(p)) # 计算熵值
g = 1 - e # 计算一致性程度
w = g / sum(g) # 计算权重
return w
# 表示各项指标为正向指标还是反向指标
flag=["+","+","+","+","+", "+","+","+","+","+",
"+","+","+","+","+", "+","+","+","+","+",
"+","+","+","+","+", "+","+","+","+","+",
"+","+","+","+","+", "+","+","+","+","+",
"+","+","+","+","+", "+","+","+","+","+",
"+","+","+","+"]
# 调用函数将数据标准化,即为每个数据添加正相关还是负相关标志
std_value=std_data(value, flag)
# 调用函数求权重
w = cal_weight(indicator, project, std_value)
w = pd.DataFrame(w, index=data.columns, columns=['权重'])
print(w)
# 调用函数求得分
score = np.dot(std_value, w).round(4) # 对应数据与权重相乘得到分数,结果保留四位小数
score = pd.DataFrame(score, index=data.index, columns=['综合得分']).sort_values(by=['综合得分'], ascending=False)
print(score)
csv下载链接 提取码:1234