• 【数学建模】Topsis法python代码


         昨天学习了Topsis法的基本概念,今天就来一起实践一下,用python实现topsis法

     代码分块解释 

     1、引入numpy库

     numpy as np

    2、输入参评与指标数目 

    1. # 用户输入参评数目和指标数目,将输入的字符串转换为数值
    2. print("请输入参评数目:")
    3. n = int(input()) # 接收参评数目,输入的数转换成int
    4. print("请输入指标数目:")
    5. m = int(input()) # 接收指标数目
    6. print("请输入类型矩阵:1.极大型,2.极小型,3.中间型,4.区间型")
    7. kind = input().split(" ")# 将输入的字符串按空格分割,形成列表

     3、接收输入的矩阵并转换成numpy数组

    1. # 接收输入的矩阵并转换成numpy数组
    2. print("请输入矩阵:")
    3. A = np.zeros(shape=(n,m)) # 初始化一个n行m列的全零矩阵A
    4. for i in range(n):
    5. A[i] = input().split(" ") # 接收每行输入的数据
    6. A[i] = list(map(float, A[i])) # 将接收到的字符串转换为浮点型列表
    7. print("输入矩阵为,\n{}".format(A))

    4.转化函数

    极小型转化 为极大型指标

    1. def minTomax(maxx, x):
    2. x = list(x)
    3. ans = [[(maxx-e)] for e in x] #列表推导式
    4. return np.array(ans)

    中间型指标转化为极大型指标

    1. def midTomax(bestx,x):
    2. x = list(x)
    3. h = [abs(e-bestx) for e in x]
    4. M = max(h)
    5. if M == 0:
    6. M = 1
    7. ans = [[(1-e/M)] for e in h]
    8. return np.array(ans)

    区间型指标转化为极大型指标

    1. def regTomax(lowx, highx, x):
    2. x = list(x)
    3. M = max(lowx-min(x), max(x)-highx)
    4. if M == 0:
    5. M = 1
    6. ans = []
    7. for i in range(len(x)):
    8. if x[i]
    9. ans.append([1-(lowx-x[i])/M])
    10. elif x[i]>highx:
    11. ans.append([1-(x[i]-highx)/M])
    12. else:
    13. ans.append([1])
    14. return np.array(ans)

     计算统一指标后的矩阵

    1. X = np.zeros(shape=(n, 1))
    2. for i in range(m):
    3. if kind[i] == "1":
    4. v = np.array(A[:, i])
    5. elif kind[i] =="2":
    6. maxA = max(A[:, i])
    7. v = minTomax(maxA, A[:, i])
    8. elif kind[i] == "3":
    9. print("类型三:请输入最优值:")
    10. bestA = eval(input())
    11. v = midTomax(bestA, A[:, i])
    12. elif kind[i] == "4":
    13. print("类型四:请输入区间【a,b】值a:")
    14. lowA = eval(input())
    15. print("类型四:请输入区间【a,b】值b:")
    16. highA = eval(input())
    17. v = regTomax(lowA, highA, A[:, i])
    18. if i==0:
    19. X = v.reshape(-1, 1)
    20. else:
    21. X = np.hstack([X, v.reshape(-1,1)])
    22. print("统一指标后矩阵为:\n{}".format(X))
    23. # 标准化
    24. X = X.astype('float')
    25. for j in range(m):
    26. X[:, j] = X[:, j]/np.sqrt(sum(X[:, j]**2))# 少加了一个”,“导致报错
    27. # 开根号
    28. print("标准化矩阵为:\n{}".format(X))

     计算距离

    1. #最大值与最小值距离的计算
    2. x_max = np.max(X, axis=0)
    3. x_min = np.min(X, axis=0)
    4. d_z = np.sqrt(np.sum(np.square((X - np.tile(x_max, (n, 1)))), axis=1))
    5. d_f = np.sqrt(np.sum(np.square((X - np.tile(x_min, (n, 1)))), axis=1))
    6. print('每个指标的最大值:',x_max)
    7. print('每个指标的最小值:',x_min)
    8. print('d+向量:',d_z)
    9. print('d-向量:',d_f)
    10. s = d_f/(d_z+d_f)
    11. Score = 100*s/sum(s)
    12. for i in range(len(Score)):
    13. print(f"第{i+1}个标准化后百分制得分为:{Score[i]}")

     完整代码

    1. import numpy as np
    2. # 用户输入参评数目和指标数目,将输入的字符串转换为数值
    3. print("请输入参评数目:")
    4. n = int(input()) # 接收参评数目,输入的数转换成int
    5. print("请输入指标数目:")
    6. m = int(input()) # 接收指标数目
    7. print("请输入类型矩阵:1.极大型,2.极小型,3.中间型,4.区间型")
    8. kind = input().split(" ")# 将输入的字符串按空格分割,形成列表
    9. # 接收输入的矩阵并转换成numpy数组
    10. print("请输入矩阵:")
    11. A = np.zeros(shape=(n,m)) # 初始化一个n行m列的全零矩阵A
    12. for i in range(n):
    13. A[i] = input().split(" ") # 接收每行输入的数据
    14. A[i] = list(map(float, A[i])) # 将接收到的字符串转换为浮点型列表
    15. print("输入矩阵为,\n{}".format(A))
    16. def minTomax(maxx, x):
    17. x = list(x)
    18. ans = [[(maxx-e)] for e in x] #列表推导式
    19. return np.array(ans)
    20. def midTomax(bestx,x):
    21. x = list(x)
    22. h = [abs(e-bestx) for e in x]
    23. M = max(h)
    24. if M == 0:
    25. M = 1
    26. ans = [[(1-e/M)] for e in h]
    27. return np.array(ans)
    28. def regTomax(lowx, highx, x):
    29. x = list(x)
    30. M = max(lowx-min(x), max(x)-highx)
    31. if M == 0:
    32. M = 1
    33. ans = []
    34. for i in range(len(x)):
    35. if x[i]
    36. ans.append([1-(lowx-x[i])/M])
    37. elif x[i]>highx:
    38. ans.append([1-(x[i]-highx)/M])
    39. else:
    40. ans.append([1])
    41. return np.array(ans)
    42. X = np.zeros(shape=(n, 1))
    43. for i in range(m):
    44. if kind[i] == "1":
    45. v = np.array(A[:, i])
    46. elif kind[i] =="2":
    47. maxA = max(A[:, i])
    48. v = minTomax(maxA, A[:, i])
    49. elif kind[i] == "3":
    50. print("类型三:请输入最优值:")
    51. bestA = eval(input())
    52. v = midTomax(bestA, A[:, i])
    53. elif kind[i] == "4":
    54. print("类型四:请输入区间【a,b】值a:")
    55. lowA = eval(input())
    56. print("类型四:请输入区间【a,b】值b:")
    57. highA = eval(input())
    58. v = regTomax(lowA, highA, A[:, i])
    59. if i==0:
    60. X = v.reshape(-1, 1)
    61. else:
    62. X = np.hstack([X, v.reshape(-1,1)])
    63. print("统一指标后矩阵为:\n{}".format(X))
    64. # 标准化
    65. X = X.astype('float')
    66. for j in range(m):
    67. X[:, j] = X[:, j]/np.sqrt(sum(X[:, j]**2))# 少加了一个”,“导致报错
    68. # 开根号
    69. print("标准化矩阵为:\n{}".format(X))
    70. #最大值与最小值距离的计算
    71. x_max = np.max(X, axis=0)
    72. x_min = np.min(X, axis=0)
    73. d_z = np.sqrt(np.sum(np.square((X - np.tile(x_max, (n, 1)))), axis=1))
    74. d_f = np.sqrt(np.sum(np.square((X - np.tile(x_min, (n, 1)))), axis=1))
    75. print('每个指标的最大值:',x_max)
    76. print('每个指标的最小值:',x_min)
    77. print('d+向量:',d_z)
    78. print('d-向量:',d_f)
    79. s = d_f/(d_z+d_f)
    80. Score = 100*s/sum(s)
    81. for i in range(len(Score)):
    82. 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