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
| import numpy as np
print("请输入参评数目:") n = eval(input()) print("请输入指标数目:") m = eval(input())
print("请输入类型矩阵:1:极大型,2:极小型,3:中间型,4:区间型") kind = input().split(" ")
print("请输入矩阵:") A = np.zeros(shape=(n, m)) for i in range(n): A[i] = input().split(" ") A[i] = list(map(float, A[i])) print("输入矩阵为:\n{}".format(A))
def minTomax(maxx, x): x = list(x) ans = [[(maxx-e)] for e in x] return np.array(ans)
def midTomax(bestx, x): x = list(x) h = [abs(e-bestx) for e in x] M = max(h) if M == 0: M = 1 ans = [[(1-e/M)] for e in h] return np.array(ans)
def regTomax(lowx, highx, x): x = list(x) M = max(lowx-min(x), max(x)-highx) if M == 0: M = 1 ans = [] for i in range(len(x)): if x[i]<lowx: ans.append([(1-(lowx-x[i])/M)]) elif x[i]>highx: ans.append([(1-(x[i]-highx)/M)]) else: ans.append([1]) return np.array(ans)
X = np.zeros(shape=(n, 1)) for i in range(m): if kind[i]=="1": v = np.array(A[:, i]) elif kind[i]=="2": maxA = max(A[:, i]) v = minTomax(maxA, A[:, i]) elif kind[i]=="3": print("类型三:请输入最优值:") bestA = eval(input()) v = midTomax(bestA, A[:, i]) elif kind[i]=="4": print("类型四:请输入区间[a, b]值a:") lowA = eval(input()) print("类型四:请输入区间[a, b]值b:") highA = eval(input()) v = regTomax(lowA, highA, A[:, i]) if i==0: X = v.reshape(-1, 1) else: X = np.hstack([X, v.reshape(-1, 1)]) print("统一指标后矩阵为:\n{}".format(X))
X = X.astype('float') for j in range(m): X[:, j] = X[:, j]/np.sqrt(sum(X[:, j]**2)) print("标准化矩阵为:\n{}".format(X))
x_max = np.max(X, axis=0) x_min = np.min(X, axis=0) d_z = np.sqrt(np.sum(np.square((X - np.tile(x_max, (n, 1)))), axis=1)) d_f = np.sqrt(np.sum(np.square((X - np.tile(x_min, (n, 1)))), axis=1)) print('每个指标的最大值:', x_max) print('每个指标的最小值:', x_min) print('d+向量:', d_z) print('d-向量:', d_f)
s = d_f/(d_z+d_f) Score = 100*s/sum(s) for i in range(len(Score)): print(f"第{i+1}个标准化后百分制得分为:{Score[i]}")
|