| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 
 | import numpy as npimport pandas as pd
 np.set_printoptions(threshold=np.inf)
 np.set_printoptions(suppress=True)
 
 
 data = pd.read_excel('收发货表格.xlsx',usecols=['发货','收货','总运输','发斜','收斜','平均相关性','发货城市','收货城市','连接数'])
 x_data = np.array(data)
 
 
 def positive(x, type, best_value=None, a=None, b=None):
 '''
 :param x: 原始数据
 :param type: 1表示极小型,2表示中间型,3表示区间型,4表示正数话,[[,1],[,2],[,3]]前面是需要正向化的列的序号
 :param best_value: 中间型的最优值
 :param a: 区间型的区间下限
 :param b: 区间型的区间上限
 :return: 正向化后的数据(列)
 '''
 if type == None:
 pass
 else:
 x = x.T
 m = np.array(type).shape[0]
 for i in range(int(m)):
 if type[i][1] == 1:
 x[type[i][0]] = x[type[i][0]].max(0)-x[type[i][0]]
 elif type[i][1] == 2:
 max_value = (abs(x[type[i][0]] - best_value)).max()
 x[type[i][0]] = 1 - abs(x[type[i][0]] - best_value) / max_value
 elif type[i][1] == 3:
 max_value = (np.append(a-x[type[i][0]].min(),x[type[i][0]].max()-b)).max()
 x_rows = x[type[i][0]].shape[0]
 for v in range(x_rows):
 if x[type[i][0]][v] > b:
 x[type[i][0]][v] = 1-(x[type[i][0]][v]-b)/max_value
 elif x[type[i][0]][v] < a:
 x[type[i][0]][v] = 1-(a-x[type[i][0]][v])/max_value
 elif a <= x[type[i][0]][v] <= b:
 x[type[i][0]][v] = 1
 else:
 x[type[i][0]][v] = 0
 elif type[i][1] == 4:
 x[type[i][0]] = 1-(x[type[i][0]].min(0)) + x[type[i][0]]
 return x.T
 
 
 def normalize(x):
 sqrt_sum = (x * x).sum(axis=0)
 sqt_sum_z = np.tile(sqrt_sum, (x.shape[0], 1))
 Z = x / np.sqrt(sqt_sum_z)
 return Z
 
 
 def importance(data):
 l = len(data[0])
 h = [0]*l
 w = [0]*l
 data = data.T
 for v in range(l):
 for i in range(len(data[0])):
 if data[v][i] == 0:
 pass
 else:
 h[v] += -data[v][i] * np.log(data[v][i])/np.log(len(data[0]))
 for i in range(l):
 w[i] = (1-h[i])/(len(data)-sum(h))
 return w
 
 def topsis(z,h):
 z_max = z.max(0)
 z_min = z.min(0)
 
 d_m = z - np.tile(z_max, (z.shape[0], 1))
 d_i = z - np.tile(z_min, (z.shape[0], 1))
 
 d_i_max = np.sqrt(((h * d_m) ** 2).sum(axis=1))
 d_i_min = np.sqrt(((h * d_i) ** 2).sum(axis=1))
 
 score = d_i_min/(d_i_max + d_i_min)
 std_score = score / score.sum(axis=0)
 return std_score
 
 if __name__ == "__main__":
 
 type = [[3,4],[4,4],[1,4],[7,4]]
 a = positive(x_data,type,best_value=None, a=None, b=None)
 
 b = normalize(a)
 
 h = importance(b)
 
 s = topsis(b,h)
 
 clo = np.array(['A','B','C','D','E','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y'])
 lianjie_list = []
 for i in range(len(s)):
 lianjie_list.append([clo[i], s[i]])
 jieguo = sorted(lianjie_list,reverse=True, key=lambda x: x[1])
 print(jieguo)
 
 
 |