TOPSIS(Python)

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]  #前一个是按结果排序结果,后一个是名次

你可能感兴趣的:(Python数学建模,python,开发语言)