Allenfenqu/TOPSIS.py

43 lines
1.1 KiB
Python

import numpy as np
# 改好的!!!
# a=[[2.07281259690102,259.262901730268],
# [2.08755045403027,439.007071864712],
# [0.0506263615854938,259.262901730268],
# [1.59371377274633,78.7012783694741],
# [0.886343572877322,59.7129172451383]]
def TOPSIS(a):
a = [[1 / x if x != 0 else 1000000 for x in row] for row in a]
m = len(a)
n = len(a[0])
x = np.zeros((m, n))
for i in range(m):
for j in range(n):
x[i][j] = a[i][j] / (np.sqrt(sum([row[j] ** 2 for row in a])))
minx = np.min(x, axis=0)
maxx = np.max(x, axis=0)
a = np.array(a) # 将列表转换为NumPy数组
Dmin = np.zeros(len(a[:,1]))
Dmax = np.zeros(len(a[:,1]))
for i in range(len(a[:,1])):
for j in range(len(a[1,:])):
Dmin[i] += (x[i,j] - minx[j])**2
Dmax[i] += (x[i,j] - maxx[j])**2
Dmin = np.sqrt(Dmin)
Dmax = np.sqrt(Dmax)
B = np.zeros(len(a[:,1]))
for i in range(len(a[:,1])):
B[i] = Dmin[i] / (Dmin[i] + Dmax[i])
m, n = np.max(B), np.argmax(B)
# print(m)
# print(n)
return m, n