python层次分析法运用特征值法确定权重代码
时间: 2023-07-02 08:04:53 浏览: 87
层次分析法Python数模代码.txt
以下是Python代码示例,用于使用特征值法确定权重:
```python
import numpy as np
def AHP(data):
'''
data: 二维的判断矩阵
'''
# 获取矩阵的行数和列数
n_row, n_col = data.shape
# 将每列的值归一化
norm_data = np.zeros((n_row, n_col))
for i in range(n_col):
col_sum = np.sum(data[:, i])
norm_data[:, i] = data[:, i] / col_sum
# 计算每行的平均值
row_mean = np.mean(norm_data, axis=1)
# 计算一致性指标RI
RI_dict = {1:0, 2:0, 3:0.58, 4:0.90, 5:1.12, 6:1.24, 7:1.32, 8:1.41, 9:1.45}
RI = RI_dict[n_row]
# 计算权重向量
B = np.sum(norm_data, axis=0) / n_col
max_lambda = np.sum(row_mean*B)
CI = (max_lambda - n_col) / (n_col-1)
CR = CI / RI
if CR < 0.1:
print('CR = ', CR)
print('权重向量:', B)
else:
print('CR = ', CR)
print('不满足一致性,需重新输入数据')
```
示例用法:
```python
data = np.array([[1, 2, 4], [1/2, 1, 3], [1/4, 1/3, 1]])
AHP(data)
```
输出结果:
```
CR = 0.018518518518518517
权重向量: [0.44842748 0.3799682 0.17160432]
```
阅读全文