数学建模——层次分析法Python代码

该博客介绍了如何利用Python实现层次分析法(AHP)来计算权重。文章中展示了AHP类的定义,包括初始化、一致性检验、三种权重计算方法(算术平均法、几何平均法、特征值法)。并通过一个具体的判断矩阵实例,演示了权重计算的全过程。

数学建模——层次分析法Python代码

import numpy as np
class AHP:
“”"
相关信息的传入和准备
“”"

def __init__(self, array):
    ## 记录矩阵相关信息
    self.array = array
    ## 记录矩阵大小
    self.n = array.shape[0]
    # 初始化RI值,用于一致性检验
    self.RI_list = [0, 0, 0.52, 0.89, 1.12, 1.26, 1.36, 1.41, 1.46, 1.49, 1.52, 1.54, 1.56, 1.58,
                    1.59]
    # 矩阵的特征值和特征向量
    self.eig_val, self.eig_vector = np.linalg.eig(self.array)
    # 矩阵的最大特征值
    self.max_eig_val = np.max(self.eig_val)
    # 矩阵最大特征值对应的特征向量
    self.max_eig_vector = self.eig_vector[:, np.argmax(self.eig_val)].real
    # 矩阵的一致性指标CI
    self.CI_val = (self.max_eig_val - self.n) / (self.n - 1)
    # 矩阵的一致性比例CR
    self.CR_val = self.CI_val / (self.RI_list[self.n - 1])

"""
一致性判断
"""

def test_consist(self):
    # 打印矩阵的一致性指标CI和一致性比例CR
    print("判断矩阵的CI值为:" + str(self.CI_val))
    print("判断矩阵的CR值为:" + str(self.CR_val))
    # 进行一致性检验判断
    if self.n == 2:  # 当只有两个子因素的情况
        print("仅包含两
层次分析法(Analytic Hierarchy Process,AHP)是一种用于多准则决策的方法,其目标是通过对准则的相对重要性进行定量评估,并对各个备选方案进行排序。下面是一个使用Python实现AHP的代码示例: ```python import numpy as np from numpy import linalg class AHP: def __init__(self, matrix): self.matrix = matrix def cal_weight_by_arithmetic_method(self): n = len(self.matrix) b = np.sum(self.matrix, axis=0) normal_a = self.matrix / b average_weight = np.mean(normal_a, axis=1) return average_weight def cal_weight_by_geometric_method(self): n = len(self.matrix) b = np.prod(self.matrix, axis=1) c = np.power(b, 1/n) average_weight = c / np.sum(c) return average_weight def cal_weight_by_eigenvalue_method(self): n = len(self.matrix) w, v = linalg.eig(self.matrix) eigenvalue = np.max(w) eigenvector = v[:, np.argmax(w)] average_weight = eigenvector / np.sum(eigenvector) return average_weight # 示例用法 if __name__ == "__main__": b = np.array([[1, 1/3, 1/8], [3, 1, 1/3], [8, 3, 1]]) ahp = AHP(b) weight1 = ahp.cal_weight_by_arithmetic_method() weight2 = ahp.cal_weight_by_geometric_method() weight3 = ahp.cal_weight_by_eigenvalue_method() ``` 这段代码实现了AHP的算术平均法、几何平均法和特征值法,可以根据输入的判断矩阵计算出相应的权重。其中,`cal_weight_by_arithmetic_method()`函数实现了算术平均法,`cal_weight_by_geometric_method()`函数实现了几何平均法,`cal_weight_by_eigenvalue_method()`函数实现了特征值法。每个方法返回的是一个代表权重的一维数组。 请注意,代码中使用了NumPy库进行矩阵运算和线性代数计算。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [数学建模——层次分析法Python代码](https://blog.youkuaiyun.com/qq_45934521/article/details/118972017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [AHP层次分析法python代码讲解(处理论文、建模)](https://blog.youkuaiyun.com/knighthood2001/article/details/127519604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FedXAI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值