基于python的数学建模---多模糊评价
权重 ak的确定——频数统计法
选取正整数p的方法
画箱形图 取1/4与3/4的距离(IQR) ceil()取整
代码:
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 | import numpy as np def frequency(matrix,p): ''' 频数统计法确定权重 :param matrix: 因素矩阵 :param p: 分组数 :return: 权重向量 ''' A = np.zeros((matrix.shape[ 0 ])) for i in range ( 0 , matrix.shape[ 0 ]): ## 根据频率确定频数区间列表 row = list (matrix[i, :]) maximum = max (row) minimum = min (row) gap = (maximum - minimum) / p row.sort() group = [] item = minimum while (item < maximum): group.append([item, item + gap]) item = item + gap print (group) # 初始化一个数据字典,便于记录频数 dataDict = {} for k in range ( 0 , len (group)): dataDict[ str (k)] = 0 # 判断本行的每个元素在哪个区间内,并记录频数 for j in range ( 0 , matrix.shape[ 1 ]): for k in range ( 0 , len (group)): if (matrix[k, j] > = group[k][ 0 ]): dataDict[ str (k)] = dataDict[ str (k)] + 1 break print (dataDict) # 取出最大频数对应的key,并以此为索引求组中值 index = int ( max (dataDict,key = dataDict.get)) mid = (group[index][ 0 ] + group[index][ 1 ]) / 2 print (mid) A[i] = mid A = A / sum (A[:]) # 归一化 return A |
权重 ak的确定——模糊层次分析法
代码:
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 | import numpy as np def AHP(matrix): if isConsist(matrix): lam, x = np.linalg.eig(matrix) return x[ 0 ] / sum (x[ 0 ][:]) else : print ( "一致性检验未通过" ) return None def isConsist(matrix): ''' :param matrix: 成对比较矩阵 :return: 通过一致性检验则返回true,否则返回false ''' n = np.shape(matrix)[ 0 ] a, b = np.linalg.eig(matrix) maxlam = a[ 0 ].real CI = (maxlam - n) / (n - 1 ) RI = [ 0 , 0 , 0.58 , 0.9 , 1.12 , 1.24 , 1.32 , 1.41 , 1.45 ] CR = CI / RI[n - 1 ] if CR < 0.1 : return True , CI, RI[n - 1 ] else : return False , None , None |
import numpy as np def appraise(criterionMatrix, targetMatrixs, relationMatrixs): ''' :param criterionMatrix: 准则层权重矩阵 :param targetMatrix: 指标层权重矩阵列表 :param relationMatrixs: 关系矩阵列表 :return: ''' R = np.zeros((criterionMatrix.shape[1], relationMatrixs[0].shape[1])) for index in range(0, len(targetMatrixs)): row = mul_mymin_operator(targetMatrixs[index], relationMatrixs[index]) R[index] = row B = mul_mymin_operator(criterionMatrix, R) return B / sum(B[:]) def mul_mymin_operator(A, R): B = np.zeros(1, R.shape[1]) for column in range(1, R.shape[1]): list = [] for row in range(1, R.shape[0]): list = list.append(A[row] * R[row, column]) B[0, column] = mymin(list) return B def mymin(list): global temp for index in range(1, len(list)): if index == 1: temp = min(1, list[0] + list[1]) else: temp = min(1, temp + list[index]) return temp
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)