TOPSIS
class TOPSIS:
def __init__(self,data:np.ndarray):
self.data = data
指标正向化
def max(self,col:int)->np.ndarray:
return self.data
def min(self,col:int)->np.ndarray:
self.data[:,col] = self.data[:,col].max() - self.data[:,col]
return self.data
def mid(self,col:int,best:float)->np.ndarray:
self.abs = np.abs((self.data[:,col] - best))
self.M = self.abs.max()
self.data[:,col] = 1 - self.abs / self.M
return self.data
def sec(self,col:int,down:float,up:float)->np.ndarray:
self.M = max(down - self.data[:,col].min(),self.data[:,col].max() - up)
for i in np.nditer(self.data[:,col],op_flags = ['readwrite']):
if i <= up and i >= down: i[...] = 1
elif i > up: i[...] = 1 - (i - up) / self.M
else: i[...] = 1 - (down - i) / self.M
return self.data
标准化处理
def normal(self):
self.Z = self.data / (self.data * self.data).sum(0)**0.5
return self.Z
结果
def ret(self)->np.ndarray:
self.D_P = ((self.Z - self.Z.max(0)) ** 2).sum(1) ** 0.5
self.D_N = ((self.Z - self.Z.min(0)) ** 2).sum(1) ** 0.5
self.S = self.D_N / (self.D_N + self.D_P)
self.stand = self.S / self.S.sum()
return [np.sort(-self.stand,0) * -1,np.argsort(-self.stand,0) + 1]