AHP的代码,Python
时间: 2023-11-19 12:49:57 浏览: 117
AHP层次分析法Python实现代码
4星 · 用户满意度95%
AHP(层次分析法)是一种用于决策的数学模型。以下是一个使用Python编写的AHP代码示例:
```python
import numpy as np
from functools import reduce
def ahp_method(dataset, wd='m'):
# 随机一致性指标R.I.(inc_rat)
inc_rat = np.array([0, 0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51, 1.48, 1.56, 1.57, 1.59])
# 数据深复制
X = np.copy(dataset)
# 生成权重向量
weights = np.zeros(X.shape[1])
# 根据权重计算方法来确定各个权重
if (wd == 'm' or wd == 'mean'):
# 均值
weights = np.mean(X / np.sum(X, axis=0), axis=1)
elif (wd == 'g' or wd == 'geometric'):
# 几何
for i in range(0, X.shape[1]):
weights[i] = reduce((lambda x, y: x * y), X[i, :])**(1 / X.shape[1])
weights = weights / np.sum(weights)
# 计算特征根向量
vector = np.sum(X * weights, axis=1) / weights
# 获得平均特征根
lamb_max = np.mean(vector)
# 计算一致性指标
cons_ind = (lamb_max - X.shape[1]) / (X.shape[1] - 1)
# 一致性判断
rc = cons_ind / inc_rat[X.shape[1]]
return weights, rc
```
阅读全文