昨天学习了Topsis法的基本概念,今天就来一起实践一下,用python实现topsis法
numpy as np
- # 用户输入参评数目和指标数目,将输入的字符串转换为数值
- print("请输入参评数目:")
- n = int(input()) # 接收参评数目,输入的数转换成int
- print("请输入指标数目:")
- m = int(input()) # 接收指标数目
- print("请输入类型矩阵:1.极大型,2.极小型,3.中间型,4.区间型")
- kind = input().split(" ")# 将输入的字符串按空格分割,形成列表
- # 接收输入的矩阵并转换成numpy数组
- print("请输入矩阵:")
- A = np.zeros(shape=(n,m)) # 初始化一个n行m列的全零矩阵A
- 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]
- 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]}")
完整代码
- import numpy as np
-
- # 用户输入参评数目和指标数目,将输入的字符串转换为数值
- print("请输入参评数目:")
- n = int(input()) # 接收参评数目,输入的数转换成int
- print("请输入指标数目:")
- m = int(input()) # 接收指标数目
- print("请输入类型矩阵:1.极大型,2.极小型,3.中间型,4.区间型")
- kind = input().split(" ")# 将输入的字符串按空格分割,形成列表
-
- # 接收输入的矩阵并转换成numpy数组
- print("请输入矩阵:")
- A = np.zeros(shape=(n,m)) # 初始化一个n行m列的全零矩阵A
- 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]
- 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]}")
-
-
相关阅读:
十家公司有九家问过的软件测试面试题,最后一题我猜你肯定不会
怎么恢复苹果回收站删除的文件呢?
验证浮点数输入
【软件测试】什么?这是最全的--金融行业测试类型细分,宝藏干G货......
COMPUTATIONAL BIOLOGYAND BIOIINFORMATICS投稿经验分享
cs224n-2022-assignment1
配置Mysql与注册登录模块
669. 修剪二叉搜索树 ●●
JSP forward动作
kubeadm搭建k8s高可用集群(keepalived+nginx+3master)
-
原文地址:https://blog.csdn.net/yf743909/article/details/136581683