相关信息的传入和准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import numpy as np class AHP: """ 相关信息的传入和准备 """
def __init__(self, array): self.array = array self.n = array.shape[0] self.RI_list = [0, 0, 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] self.eig_val, self.eig_vector = np.linalg.eig(self.array) self.max_eig_val = np.max(self.eig_val) self.max_eig_vector = self.eig_vector[:, np.argmax(self.eig_val)].real self.CI_val = (self.max_eig_val - self.n) / (self.n - 1) self.CR_val = self.CI_val / (self.RI_list[self.n - 1])
|
一致性判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| def test_consist(self): print("判断矩阵的CI值为:" + str(self.CI_val)) print("判断矩阵的CR值为:" + str(self.CR_val)) if self.n == 2: print("仅包含两个子因素,不存在一致性问题") else: if self.CR_val < 0.1: print("判断矩阵的CR值为" + str(self.CR_val) + ",通过一致性检验") return True else: print("判断矩阵的CR值为" + str(self.CR_val) + "未通过一致性检验") return False
|
算术平均法求权重
1 2 3 4 5 6 7 8 9 10 11
| def cal_weight_by_arithmetic_method(self): col_sum = np.sum(self.array, axis=0) array_normed = self.array / col_sum array_weight = np.sum(array_normed, axis=1) / self.n print("算术平均法计算得到的权重向量为:\n", array_weight) return array_weight
|
几何平均法求权重
1 2 3 4 5 6 7 8 9 10 11
| def cal_weight__by_geometric_method(self): # 求矩阵的每列的积 col_product = np.product(self.array, axis=0) # 将得到的积向量的每个分量进行开n次方 array_power = np.power(col_product, 1 / self.n) # 将列向量归一化 array_weight = array_power / np.sum(array_power) # 打印权重向量 print("几何平均法计算得到的权重向量为:\n", array_weight) # 返回权重向量的值 return array_weight
|
特征值法求权重
def cal_weight__by_eigenvalue_method(self):
# 将矩阵最大特征值对应的特征向量进行归一化处理就得到了权重
array_weight = self.max_eig_vector / np.sum(self.max_eig_vector)
# 打印权重向量
print("特征值法计算得到的权重向量为:\n", array_weight)
# 返回权重向量的值
return array_weight
给出判断矩阵
1 2 3 4 5 6 7 8 9
| if __name__ == "__main__": b = np.array([[1, 1 / 3, 1 / 8], [3, 1, 1 / 3], [8, 3, 1]])
weight1 = AHP(b).cal_weight_by_arithmetic_method() weight2 = AHP(b).cal_weight__by_geometric_method() weight3 = AHP(b).cal_weight__by_eigenvalue_method()
|