1 2 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
import numpy as np
n = 3
m = 4
kind = [1,2,3,4]
A = np.array([[9,10,175,120],[8,7,164,80],[6,3,157,90]])
def mintomax(maxx,x): x = list(x) res = [[(maxx - i)] for i in x] return np.array(res)
def midtomax(bestx,x): x = list(x) h = [abs(i - bestx) for i in x] m = max(h) if m == 0: m = 1 res = [[(1-i/m)] for i in h] return np.array(res)
def regtomax(minx,maxx,x): x = list(x) m = max(minx-min(x),max(x)-maxx) res = [] if m == 0: m = 1 for i in x: if(i < minx): res.append([1-(minx-i)/m]) elif(i > maxx): res.append([1-(i-maxx)/m]) else: res.append([1]) return np.array(res)
X = np.zeros((n,m)) for i in range(m): if kind[i] == 1: v = np.array(A[:,i]) elif kind[i] == 2: v = mintomax(max(A[:,i]),A[:,i]) elif kind[i] == 3: bestA = 165 v = midtomax(bestA,A[:,i]) elif kind[i] == 4: minx = 90 maxx = 100 v = regtomax(minx,maxx,A[:,i]) if i == 0 : X = v.reshape(-1,1) else: X = np.hstack((X,v.reshape(-1,1))) print("正向化矩阵为:\n",X)
for j in range(m): X[:,j] = X[:,j]/np.sqrt(sum(X[:,j]**2))
print("标准化矩阵为:\n",X)
xmax = np.max(X,axis=0) xmin = np.min(X,axis=0)
d_z = np.sqrt(np.sum(np.square((X-np.tile(xmax,(n,1)))),axis=1))
d_f = np.sqrt(np.sum(np.square((X-np.tile(xmin,(n,1)))),axis=1))
print("每个指标的最大值:",xmax) print("每个指标的最小值:",xmin) print("d+向量:",d_z) print("d-向量:",d_f)
s = d_f/(d_z+d_f) s = s/sum(s) s *= 100 for i in range(len(s)): print(f"第{i+1}个对象的评分为{s[i]}")
|