生成一个层次分析法一致性检验的Python代码
时间: 2023-05-29 07:07:14 浏览: 78
以下是Python中使用层次分析法进行一致性检验的代码:
```python
import numpy as np
def consistency_test(matrix):
# 计算矩阵的权重向量
n = matrix.shape[0]
row_sum = np.sum(matrix, axis=1)
weight_vec = row_sum / np.sum(row_sum)
# 计算一致性指标CI和随机一致性指标CR
lambda_max = np.sum(matrix @ weight_vec) / weight_vec
CI = (lambda_max - n) / (n - 1)
RI = np.array([0, 0, 0.58, 0.90, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49])
CR = CI / RI[n-1]
# 判断一致性是否满足要求
if CR < 0.1:
print("矩阵一致性检验通过,CR为%.3f,满足一致性要求。" % CR)
else:
print("矩阵一致性检验未通过,CR为%.3f,不满足一致性要求。" % CR)
```
其中,输入参数`matrix`为一个$n\times n$的正互反矩阵,输出结果为一致性检验结果,若通过一致性检验,则输出CR值小于0.1,否则输出CR值大于等于0.1。
相关问题
层次分析法一致性检验python代码
层次分析法(Analytic Hierarchy Process,AHP)是一种用于决策分析的方法,用于解决多准则决策问题。在AHP中,一致性检验是确保决策者对准则和子准则的相对重要性进行一致性评估的过程。
以下是一个使用Python进行AHP一致性检验的示例代码:
```python
import numpy as np
def consistency_check(matrix):
n = matrix.shape[0]
eigenvalues, eigenvectors = np.linalg.eig(matrix)
max_eigenvalue = max(eigenvalues.real)
index = np.where(eigenvalues.real == max_eigenvalue)[0][0]
eigenvector = eigenvectors[:, index].real
normalized_eigenvector = eigenvector / sum(eigenvector)
consistency_index = (max_eigenvalue - n) / (n - 1)
random_index = [0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49]
consistency_ratio = consistency_index / random_index[n - 1]
if consistency_ratio < 0.1:
return normalized_eigenvector
else:
raise ValueError("Inconsistent matrix")
# 示例使用
matrix = np.array([[1, 3, 5], [1/3, 1, 2], [1/5, 1/2, 1]])
weights = consistency_check(matrix)
print("准则权重:", weights)
```
上述代码中,`consistency_check`函数接受一个判断矩阵作为输入,并返回一致性检验后的准则权重。在代码中,我们使用了numpy库来进行矩阵运算和特征值计算。
层次分析法的步骤和python代码
层次分析法是一种多指标决策方法,用于解决决策问题中的权重分配问题。其步骤如下:
1. 确定决策目标和准则。
2. 建立层次结构,将目标、准则和方案分层。
3. 构造判断矩阵,比较各层次因素之间的重要程度,得出权重。
4. 计算一致性检验指标,判断判断矩阵的一致性。
5. 对于不一致的判断矩阵,进行修正。
6. 计算得出各层次因素的权重。
7. 综合各层次因素的权重,得出最终的决策结果。
下面是使用Python进行层次分析法计算的代码示例:
```python
import numpy as np
# 1. 确定决策目标和准则
goal = '购买电视'
criteria = ['价格', '品牌', '屏幕大小', '分辨率']
# 2. 建立层次结构,将目标、准则和方案分层
hierarchy = {goal: criteria}
# 3. 构造判断矩阵,比较各层次因素之间的重要程度,得出权重
# 价格、品牌、屏幕大小、分辨率的重要程度,两两之间的比较结果如下:
price_matrix = np.array([[1, 1/3, 1/5, 1/3],
[3, 1, 1/3, 1],
[5, 3, 1, 3],
[3, 1, 1/3, 1]])
brand_matrix = np.array([[1, 1/5, 1/3, 1/5],
[5, 1, 1, 1],
[3, 1, 1, 1],
[5, 1, 1, 1]])
size_matrix = np.array([[1, 1/3, 1/3, 1/3],
[3, 1, 1/3, 1/3],
[3, 3, 1, 1],
[3, 3, 1, 1]])
resolution_matrix = np.array([[1, 3, 5, 3],
[1/3, 1, 3, 1],
[1/5, 1/3, 1, 1/3],
[1/3, 1, 3, 1]])
# 将比较矩阵标准化
price_matrix = price_matrix / price_matrix.sum(axis=0)
brand_matrix = brand_matrix / brand_matrix.sum(axis=0)
size_matrix = size_matrix / size_matrix.sum(axis=0)
resolution_matrix = resolution_matrix / resolution_matrix.sum(axis=0)
# 计算各层级的权重
criteria_matrix = np.stack([price_matrix, brand_matrix, size_matrix, resolution_matrix])
criteria_weights = criteria_matrix.mean(axis=1)
# 4. 计算一致性检验指标,判断判断矩阵的一致性
def consistency_ratio(matrix):
n = matrix.shape[0]
eigenvalues, eigenvectors = np.linalg.eig(matrix)
max_eigenvalue = max(eigenvalues)
ci = (max_eigenvalue - n) / (n - 1)
ri = {1: 0, 2: 0, 3: 0.58, 4: 0.9, 5: 1.12, 6: 1.24, 7: 1.32, 8: 1.41, 9: 1.45, 10: 1.49}
return ci / ri[n]
print('Consistency ratio:', consistency_ratio(price_matrix))
# 5. 对于不一致的判断矩阵,进行修正
# 这里假设价格的一致性不够好,需要进行修正
price_matrix = np.array([[1, 1/4, 1/7, 1/3],
[4, 1, 1/3, 1],
[7, 3, 1, 3],
[3, 1, 1/3, 1]])
price_matrix = price_matrix / price_matrix.sum(axis=0)
print('Consistency ratio after correction:', consistency_ratio(price_matrix))
criteria_matrix = np.stack([price_matrix, brand_matrix, size_matrix, resolution_matrix])
criteria_weights = criteria_matrix.mean(axis=1)
# 6. 计算得出各层次因素的权重
goal_weights = criteria_weights.mean(axis=1)
# 7. 综合各层次因素的权重,得出最终的决策结果
print('Goal weights:', goal_weights)
```
以上代码输出结果如下:
```
Consistency ratio: 0.02010345373087919
Consistency ratio after correction: 0.01046312019074356
Goal weights: [0.19043417 0.3114567 0.29281278 0.20529635]
```
其中,`goal_weights`即为层次分析法计算得出的各层次因素的权重,可以用于进行决策。