熵权topsis法python代码
时间: 2023-07-27 08:08:27 浏览: 398
以下是使用熵权TOPSIS法的Python代码示例:
```python
import numpy as np
import pandas as pd
# 计算熵权
def entropyWeight(data):
p = data / data.sum()
entropy = (-p * np.log2(p)).sum()
weight = (1 - entropy) / (len(data) - 1)
return weight
# 极小型指标转为极大型
def dataDirection_1(data):
return 1 / data
# 区间型指标转为极大型
def dataDirection_3(data, a, b, c, d):
return (data - a) / (b - a) * (d - c) + c
# 计算得分并排序(人工赋权重)
def topsis(data, weight=None):
Z = pd.DataFrame(\[data.max(), data.min()\], index=\['正理想解', '负理想解'\]) # 最优最劣方案(最大值Z^+ 和 最小值)
weight = entropyWeight(data) if weight is None else np.array(weight)
Result = data.copy()
Result\['正理想解'\] = np.sqrt(((data - Z.loc\['正理想解'\]) ** 2 * weight).sum(axis=1)) # 评价对象与最大值的距离
Result\['负理想解'\] = np.sqrt(((data - Z.loc\['负理想解'\]) ** 2 * weight).sum(axis=1)) # 评价对象与最小值的距离
Result\['综合得分指数'\] = Result\['负理想解'\] / (Result\['负理想解'\] + Result\['正理想解'\]) # 综合得分指数
Result\['排序'\] = Result.rank(ascending=False)\['综合得分指数'\]
return Result, Z, weight
# 例题数据处理
data = pd.DataFrame({
'人均专著': \[0.1, 0.2, 0.4, 0.9, 1.2\],
'生师比': \[5, 6, 7, 10, 2\],
'科研经费': \[5000, 6000, 7000, 10000, 400\],
'逾期毕业率': \[4.7, 5.6, 6.7, 2.3, 1.8\]
}, index=\['院校' + i for i in list('ABCDE')\])
# 极小型指标转为极大型
minimum_list = dataDirection_1(data.loc\[:, "逾期毕业率"\])
minimum_array = np.array(minimum_list)
minimum_4f = np.round(minimum_array, 6)
# 区间型指标转为极大型
maximum_list = dataDirection_3(data.loc\[:, "生师比"\], 5, 6, 2, 12)
maximum_array = np.array(maximum_list)
maximum_4f = np.round(maximum_array, 6)
# 指标正向化结果
index_Isotropy = pd.DataFrame()
index_Isotropy\["人均专著"\] = data\["人均专著"\]
index_Isotropy\["生师比"\] = maximum_4f
index_Isotropy\["科研经费"\] = data\["科研经费"\]
index_Isotropy\["逾期毕业率"\] = minimum_4f
# 人工赋权重的结果
weight = \[0.2, 0.3, 0.4, 0.1\]
Result, Z, weight = topsis(index_Isotropy, weight)
```
这段代码实现了熵权TOPSIS法的计算过程,包括指标转换、熵权计算、得分计算和排序。最终的结果存储在`Result`中,最优最劣方案存储在`Z`中,权重存储在`weight`中。
#### 引用[.reference_title]
- *1* *2* *3* [TOPSIS(优劣解距离法)【附Python实现代码及可视化代码】](https://blog.csdn.net/Michale_L/article/details/127832863)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文
相关推荐















