层次分析法Python代码
时间: 2023-11-05 17:19:40 浏览: 104
层次分析法Python代码.txt
层次分析法(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.csdn.net/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.csdn.net/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 ]
阅读全文