首先我们需要知道的是TOPSIS的基本算法和公式,在这里我不做详细介绍,大家可以到专业书籍上查找关于TOPSIS原理的具体介绍和推导过程
我总结的TOPSIS算法过程为
1.数据引入
2.数据类型判断
3.数据标准化处理
4.数据赋权处理
5.寻找正理想解和负理想解(即每列项最大值和每项最小值)
6.分别计算每项数据与正理想解和负理想解的距离
7.用与负理想解的距离除以数据到正、负理想解的距离算出最终的评价标准值
8.根据评价标准值排序
如下所示是我们的测试数据:以“优劣解距离法_测试数据.xlsx”命名
(注:相关的代码和数据在文章最后会给出链接)
代码如下:
import pandas as pd
data = pd.read_excel(r'优劣解距离法_测试数据.xlsx')
print(data.shape[0])
print(data.shape[1])
# print(data.loc[0][0])
print(data)
print('max',data['效益型'].max())
#数据处理:
#标准0-1变换:
#1.效益型:
max = data['效益型'].max()
min = data['效益型'].min()
for i in range(data.shape[0]):
data.loc[i,'效益型'] = (data.loc[i][0]-min)/(max-min)
print(data)
#2.成本型:
max = data['成本型'].max()
min = data['成本型'].min()
for i in range(data.shape[0]):
data.loc[i,'成本型'] = (max-data.loc[i][1])/(max-min)
print(data)
#3.区间型
#最佳区间长度:[5,6]
perfect_lowwer = 5
perfect_upper = 6
#可容忍区间长度[2,12]
unbearable_lowwer = 2
unbearable_upper = 12
for i in range(data.shape[0]):
if data.loc[i][2] >=unbearable_lowwer and data.loc[i][2]<perfect_lowwer:
data.loc[i,'区间型'] = 1-((perfect_lowwer-data.loc[i][2])/(perfect_lowwer-unbearable_lowwer))
elif data.loc[i][2]>=perfect_lowwer and data.loc[i][2]<=perfect_upper:
data.loc[i,'区间型'] = 1
elif data.loc[i][2]>=perfect_upper and data.loc[i][2]<=unbearable_upper:
data.loc[i,'区间型'] = 1-((data.loc[i][2]-perfect_upper)/(unbearable_upper-perfect_upper))
else:
data.loc[i,'区间型'] = 0
print(data)
#接下来给各个判断标准赋上权重值,比如我们这里以[0.2,0.5,0.3]作为我们的权重值
for i in range(data.shape[0]):
data.loc[i,'效益型'] = 0.2*data.iloc[i]['效益型']
data.loc[i,'成本型'] = 0.5*data.iloc[i]['成本型']
data.loc[i,'区间型'] = 0.3*data.iloc[i]['区间型']
print(data)
#至此,数据处理基本完成
#计算正理想解和负理想解
max_0 = data['效益型'].max()
max_1 = data['成本型'].max()
max_2 = data['区间型'].max()
min_0 = data['效益型'].min()
min_1 = data['成本型'].min()
min_2 = data['区间型'].min()
#更新为每一项与正理想解,负理想解之间的距离
s_positive = []
s_negative = []
for i in range(data.shape[0]):
s_positive.append(((data.loc[i][0]-max_0)**2 + (data.loc[i][1]-max_1)**2 + (data.loc[i][2]-max_2)**2)**0.5)
for i in range(data.shape[0]):
s_negative.append(((data.loc[i][0] - min_0) ** 2 + (data.loc[i][1] - min_1) ** 2 + (data.loc[i][2] - min_2) ** 2) ** 0.5)
#计算最总评价值judge_list
judge_list = []
for i in range(data.shape[0]):
judge_list.append(s_negative[i]/(s_negative[i]+s_positive[i]))
print(judge_list)
index_list=[i[0] for i in sorted(enumerate(judge_list), key=lambda x:x[1])] #根据f数值大小对f的索引进行排序
print(index_list) #得到最终结果
代码及数据文件如下
链接:https://pan.baidu.com/s/1Sewq56NZyubd5-QnvYPj_w
提取码:mv28
--来自百度网盘超级会员V2的分享