TOPSIS理想解法 python
时间: 2023-12-16 13:28:30 浏览: 130
熵权TOPSIS的Python代码
TOPSIS(Technique for Order Preference by Similarity to Ideal Solution)是一种多属性决策分析方法,用于解决具有多个指标的决策问题。在Python中,我们可以使用以下代码实现TOPSIS理想解法:
```python
import pandas as pd
import numpy as np
def topsis(path, index, weight, postive):
# 读取数据
data = pd.read_excel(path, index_col=index)
# 数据归一化
data_norm = data.apply(lambda x: x / np.sqrt(np.sum(x ** 2)), axis=0)
# 计算加权后的矩阵
data_weight = data_norm.apply(lambda x: x * weight, axis=1)
# 确定正负理想解
ideal_best = np.max(data_weight, axis=0)
ideal_worst = np.min(data_weight, axis=0)
for i in range(len(postive)):
if postive[i] == 0:
ideal_best[i] = ideal_worst[i]
ideal_worst[i] = np.max(data_weight.iloc[:, i])
# 计算距离
distance_best = np.sqrt(np.sum((data_weight - ideal_best) ** 2, axis=1))
distance_worst = np.sqrt(np.sum((data_weight - ideal_worst) ** 2, axis=1))
# 计算综合得分
score = distance_worst / (distance_best + distance_worst)
# 输出结果
result = pd.DataFrame({'score': score}, index=data.index)
result = result.sort_values(by='score', ascending=False)
return result
```
其中,path为数据文件路径,index为数据文件中作为索引的列的编号(从0开始),weight为各指标的权重,postive为正理想解的指标编号(从0开始)。使用该函数可以得到各个决策方案的得分,得分越高表示该方案越优。
阅读全文