清风数学建模TOPSIS优劣解距离法Python代码实现

文章详细展示了如何使用numpy进行矩阵标准化(如z-score标准化)和根据不同指标类型调整数据分布(极小型、中间型、区间型),以及计算得分和归一化的过程。
摘要由CSDN通过智能技术生成
import numpy as np
np.set_printoptions(precision=4)

# 标准化矩阵
def standardize(matrix):
    column_sums = np.sum(matrix ** 2, axis=0) #求列的元素的平方和
    n = len(matrix)
    copied_row = np.tile(column_sums, (n, 1))
    new_matrix = copied_row ** 0.5
    return matrix/new_matrix  #a矩阵除以b矩阵等于a矩阵里面的元素除以b矩阵里面对应位置的元素

# 计算得分并归一化
def calculate_scores(a):
    max_values = np.max(a, axis=0) #求出每一列的最大值,返回一个最大值向量
    min_values = np.min(a, axis=0)

    D1 = np.linalg.norm(max_values - a, axis=1)
    D0 = np.linalg.norm(min_values - a, axis=1)

    B = D0 / (D0 + D1)

    normalized_scores = B / np.sum(B)

    return normalized_scores

#将矩阵正向化
# 极小型转为极大型指标
def dataDirection_1(matrix,column_index): #操作的列的索引column_index
    # 找到该列的最大值
    max_value = np.max(matrix[:, column_index])
    # 用最大值减去该列的原始数值,并将结果直接覆盖到原始矩阵的对应列
    matrix[:, column_index] = max_value - matrix[:, column_index]
    return matrix

# 中间型指标转为极大型指标
def dataDirection_2(matrix,column_index,best_value):
    max = np.max(matrix[:, column_index])
    min = np.min(matrix[:, column_index])
    M = 0
    if abs(max - best_value) > abs(best_value - min):
        M = abs(max - best_value)
    else:
        M = abs(best_value - min)
    matrix[:, column_index] = 1 - abs(matrix[:, column_index] - best_value) / M
    return matrix

# 区间型指标转为极大型指标
#  [a, b]表示合法区间
def dataDirection_3(matrix, column_index, a, b):
    column_iterator = np.nditer(matrix[:, column_index])
    max = np.max(matrix[:, column_index])
    min = np.min(matrix[:, column_index])
    M = max - b if max - b > a - min else a - min
    cur = []
    for item in column_iterator:
        if item >= a and item <= b:
            cur.append(1)
        elif item < a:
            cur.append(1 - (a - item) / M)
        elif item > a:
            cur.append(1 - (item - b) / M)
    matrix[:, column_index] = cur
    return matrix

def data_direction(matrix):
    num_cols = int(input("请输入需要正向化的列数:"))
    nums = []
    types = []

    for i in range(num_cols):
        col_index = int(input(f"请输入第{i + 1}列的索引:"))
        nums.append(col_index)
        col_type = int(input(f"请输入第{i + 1}列的指标类型:\n1.极小型指标\n2.中间型指标\n3.区间型指标\n"))
        types.append(col_type)

    for i in range(len(types)):
        if types[i] == 1:
            matrix = dataDirection_1(matrix, nums[i])
        elif types[i] == 2:
            xbest = float(input(f"请输入第{i + 1}列最优解的值:"))
            matrix = dataDirection_2(matrix, nums[i], xbest)
        else:
            lower_bound = float(input(f"请输入第{i + 1}列合法区间的下界:"))
            upper_bound = float(input(f"请输入第{i + 1}列合法区间的上界:"))
            matrix = dataDirection_3(matrix, nums[i], lower_bound, upper_bound)
        print(f"第{i + 1}列正向化完成")
    return matrix

# matrix = np.array([[89,2],[60,0],[74,1],[99,3]])
matrix = np.array([[4.69, 6.59, 51, 11.94],
    [2.03, 7.86, 19, 6.46],
    [9.11, 6.31, 46, 8.91],
    [8.61, 7.05, 46, 26.43],
    [7.13, 6.5, 50, 23.57],
    [2.39, 6.77, 38, 24.62],
    [7.69, 6.79, 38, 6.01],
    [9.3, 6.81, 27, 31.57],
    [5.45, 7.62, 5, 18.46],
    [6.19, 7.27, 17, 7.51],
    [7.93, 7.53, 9, 6.52],
    [4.4, 7.28, 17, 25.3],
    [7.46, 8.24, 23, 14.42],
    [2.01, 5.55, 47, 26.31],
    [2.04, 6.4, 23, 17.91],
    [7.73, 6.14, 52, 15.72],
    [6.35, 7.58, 25, 29.46],
    [8.29, 8.41, 39, 12.02],
    [3.54, 7.27, 54, 3.16],
    [7.44, 6.26, 8, 28.41]])

print("正向化后的矩阵:")
a = data_direction(matrix)
print(a)
m = standardize(a)
print('标准化后的矩阵:')
print(m)
print('标准化后的矩阵的归一化得分:')
print(calculate_scores(m))

部分运行结果:

  • 25
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值