在层次分析法中,通过 算术平均法、几何平均法、特征值法 计算指标权重,再通过 一致性检验 确保判断矩阵逻辑合理,为多准则决策提供量化依据。
import numpy as np
# 1. 定义判断矩阵
A = np.array([[1, 2, 3, 5], [1/2, 1, 1/2, 2], [1/3, 2, 1, 1/2], [1/5, 1/2, 1/2, 1]])
# 2. 获取矩阵阶数(指标数量)
n = A.shape[0]
# 3. 计算特征值与特征向量
eig_val, eig_vec = np.linalg.eig(A)
Max_eig = max(eig_val) # 提取最大特征值
# 4. 计算一致性指标
CI = (Max_eig - n) / (n - 1)
# 5. 平均随机一致性指标(查表值)
RI = [0, 0.0001, 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]
CR = CI / RI[n-1] # 一致性比率
# 6. 输出结果与判断
print(f'一致性指标CI={CI}')
print(f'一致性比例CR={CR}')
if CR < 0.1:
print('CR<0.1,判断矩阵一致,可继续计算权重!')
else:
print('CR≥0.1,矩阵需调整!')
n
的关系,量化矩阵一致性。CR<0.1
是判断矩阵合理的标准。import numpy as np
# 1. 定义判断矩阵
A = np.array([[1, 2, 3, 5], [1/2, 1, 1/2, 2], [1/3, 2, 1, 1/2], [1/5, 1/2, 1/2, 1]])
# 2. 计算每列和(按列求和)
ASum = np.sum(A, axis=0)
# 3. 列归一化(矩阵元素÷对应列和)
Stand_A = A / ASum
# 4. 按行求和(归一化后行累加)
ASumr = np.sum(Stand_A, axis=1)
# 5. 计算权重(行和÷指标数)
weights = ASumr / A.shape[0]
print('算术平均法权重:', weights)
import numpy as np
# 1. 定义判断矩阵
A = np.array([[1, 2, 3, 5], [1/2, 1, 1/2, 2], [1/3, 2, 1, 1/2], [1/5, 1/2, 1/2, 1]])
# 2. 按行求元素乘积
prod_A = np.prod(A, axis=1)
# 3. 计算行乘积的 n 次根(n 是指标数)
prod_n_A = np.power(prod_A, 1/A.shape[0])
# 4. 归一化(根值÷所有根值的和)
weights = prod_n_A / np.sum(prod_n_A)
print('几何平均法权重:', weights)
import numpy as np
# 1. 定义判断矩阵
A = np.array([[1, 2, 3, 5], [1/2, 1, 1/2, 2], [1/3, 2, 1, 1/2], [1/5, 1/2, 1/2, 1]])
# 2. 计算特征值与特征向量
eig_values, eig_vectors = np.linalg.eig(A)
# 3. 找到最大特征值的索引
max_index = np.argmax(eig_values)
# 4. 提取对应特征向量并归一化
max_vector = eig_vectors[:, max_index]
weights = max_vector / np.sum(max_vector)
print('特征值法权重:', weights)