构造判断(成对比较)矩阵
第二层A的各个因素对目标层Z的成对比较矩阵(专家打分法)
说明C2的重要性是C1的两倍
第三层B的各个因素A1,A2……A5的成对比较矩阵分别如下:
层次单排序及其一致性检验
0.6=1/(1+1/2+1/6) 矩阵中其他数值同理
0.587=1/3(0.6+0.615+0.545)
(本程序配的案例,现在是机密,先不能公开,但看程序仍可以理解)
import numpy as np
# A--D的判断矩阵
A_D = np.array([
[1, 2, 6, 4],
[1 / 2, 1, 5, 2],
[1 / 6, 1 / 5, 1, 1 / 2],
[1 / 4, 1 / 2, 2, 1]
])
# A1-A4的判断矩阵
A1_A4 = np.array([
[1, 4, 5, 4],
[1 / 4, 1, 4, 2],
[1 / 5, 1 / 4, 1, 1 / 3],
[1 / 4, 1 / 2, 3, 1]
])
# B1-B3的判断矩阵
B1_B3 = np.array([
[1, 2, 3],
[1 / 2, 1, 3],
[1 / 3, 1 / 3, 1]
])
# C1--C2
C1_C2 = np.array([
[1, 2],
[1 / 2, 1]
])
# D1--D2
D1_D2 = np.array([
[1, 1],
[1, 1]
])
# A11_A14的判断矩阵
A11_A14 = np.array([
[1, 1 / 5, 1 / 4, 1 / 2],
[5, 1, 2, 3],
[4, 1 / 2, 1, 2],
[2, 1 / 3, 1 / 2, 1]
])
# A1的评价矩阵
A1_Emat = np.array([
[0.109, 0.176, 0.142, 0.302, 0.27],
[0.077, 0.062, 0.145, 0.324, 0.392],
[0.057, 0.118, 0.159, 0.34, 0.327],
[0.074, 0.173, 0.221, 0.402, 0.131]
])
# A21--A22
A21_A22 = np.array([
[1, 1 / 2],
[2, 1]
])
# A2的评价矩阵
A2_Emat = np.array([
[0.182, 0.198, 0.245, 0.265, 0.11],
[0.133, 0.147, 0.126, 0.318, 0.276]
])
# A31--A33
A31_A33 = np.array([
[1, 2, 1],
[1 / 2, 1, 1 / 2],
[1, 2, 1]
])
# A3的评价矩阵
A3_Emat = np.array([
[0.088, 0.206, 0.234, 0.265, 0.207],
[0.093, 0.129, 0.25, 0.34, 0.188],
[0.007, 0.255, 0.221, 0.341, 0.176]
])
# A41--A42
A41_A42 = np.array([
[1, 1 / 2],
[2, 1]
])
# A4的评价矩阵
A4_Emat = np.array([
[0.124, 0.211, 0.235, 0.248, 0.182],
[0.098, 0.182, 0.203, 0.302, 0.214]
])
# B11--B13
B11_B13 = np.array([
[1, 1 / 2, 1 / 3],
[2, 1, 1 / 2],
[3, 2, 1]
])
# B1的评价矩阵
B1_Emat = np.array([
[0.067, 0.194, 0.195, 0.193, 0.351],
[0.06, 0.161, 0.191, 0.206, 0.382],
[0.097, 0.134, 0.233, 0.353, 0.284]
])
# B21--B23
B21_B23 = np.array([
[1, 1 / 2, 1 / 3],
[2, 1, 1 / 2],
[3, 2, 1]
])
# B2的评价矩阵
B2_Emat = np.array([
[0.098, 0.182, 0.303, 0.302, 0.114],
[0.074, 0.163, 0.221, 0.312, 0.231],
[0.082, 0.198, 0.145, 0.365, 0.21]
])
# B31--B33
B31_B33 = np.array([
[1, 1 / 4, 1 / 3],
[4, 1, 2],
[3, 1 / 2, 1]
])
# B3的评价矩阵
B3_Emat = np.array([
[0.06, 0.157, 0.335, 0.312, 0.136],
[0.007, 0.199, 0.223, 0.355, 0.216],
[0.065, 0.054, 0.186, 0.391, 0.304]
])
# C11--C12
C11_C12 = np.array([
[1, 3 / 2],
[2 / 3, 1]
])
# C1的评价矩阵
C1_Emat = np.array([
[0.093, 0.225, 0.331, 0.249, 0.101],
[0.093, 0.213, 0.335, 0.265, 0.095]
])
# C21--C23
C21_C23 = np.array([
[1, 3, 1 / 3],
[1 / 3, 1, 1 / 5],
[3, 5, 1]
])
# C2的评价矩阵
C2_Emat = np.array([
[0.06, 0.161, 0.291, 0.306, 0.182],
[0.097, 0.234, 0.333, 0.253, 0.084],
[0.067, 0.194, 0.295, 0.293, 0.151]
])
# D11--D12
D11_D12 = np.array([
[1, 4 / 3],
[3 / 4, 1]
])
# D1的评价矩阵
D1_Emat = np.array([
[0.074, 0.163, 0.221, 0.312, 0.231],
[0.097, 0.234, 0.233, 0.253, 0.184]
])
# D21--D23
D21_D23 = np.array([
[1, 2, 2],
[1 / 2, 1, 1 / 2],
[1 / 2, 2, 1]
])
# D2的评价矩阵
D2_Emat = np.array([
[0.079, 0.198, 0.321, 0.29, 0.112],
[0.093, 0.199, 0.35, 0.278, 0.08],
[0.097, 0.234, 0.333, 0.253, 0.084]
])
def Eigenvalues_Feature_vector(phalanx):
# 计算判断矩阵的特征值和特征向量
a, b = np.linalg.eig(phalanx)
# print("特征值是\n", a)
print("特征值实部:", a.real) # 显示特征值实部
max_eigenvalue = max(a.real)
print("最大特征值:", max_eigenvalue)
num_shape = phalanx.shape
CI = (max_eigenvalue - num_shape[0]) / (num_shape[0] - 1)
print("---->> CI=", CI)
if num_shape[0] == 2:
RI = 0
elif num_shape[0] == 3:
RI = 0.52
else:
RI = 0.89
if RI == 0 and CI == 0:
CR = 0
print("---->> CR=", CR)
else:
CR = CI / RI
print("---->> CR=", CR)
if CR < 0.1:
print("---->> 一致性比例可接受!")
else:
print("---->> 一致性检验不通过!")
print("\n")
# print("特征向量是\n", b) # numpy的特征向量是竖方向的,numpy输出的特征向量是单位化后的向量
print("特征向量实部:", b.real)
return b
def Weight_vector(phalanx):
# 计算判断矩阵的权向量
num = 0
b = Eigenvalues_Feature_vector(phalanx)
for i in range(len(b.real)):
num += b.real[i][0]
print("\n")
print("指标权重是:")
weight_list = []
for j in range(len(b.real)):
weight_num = b.real[j][0] / num
weight_list.append(weight_num)
print(weight_list)
print("\n")
return np.array([weight_list])
def get_point(phalanx, Emat):
# 计算得分
vec = Weight_vector(phalanx)
Eva = np.dot(vec, Emat)
print(Eva)
grade = np.array([[20, 40, 60, 80, 100]])
Eva_point = np.dot(Eva, grade.T)
print("评价得分:----------->>", Eva_point[0][0])
print("*" * 80)
return Eva_point[0][0]
A_D_weightmat = Weight_vector(A_D)
A1_A4_weightmat = Weight_vector(A1_A4)
B1_B3_weightmat = Weight_vector(B1_B3)
C1_C2_weightmat = Weight_vector(C1_C2)
D1_D2_weightmat = Weight_vector(D1_D2)
point_A1 = get_point(A11_A14, A1_Emat)
point_A2 = get_point(A21_A22, A2_Emat)
point_A3 = get_point(A31_A33, A3_Emat)
point_A4 = get_point(A41_A42, A4_Emat)
point_B1 = get_point(B11_B13, B1_Emat)
point_B2 = get_point(B21_B23, B2_Emat)
point_B3 = get_point(B31_B33, B3_Emat)
point_C1 = get_point(C11_C12, C1_Emat)
point_C2 = get_point(C21_C23, C2_Emat)
point_D1 = get_point(D11_D12, D1_Emat)
point_D2 = get_point(D21_D23, D2_Emat)
def standard_layer_score(weightmat, point1, point2, point3=0, point4=0):
# 标准层的得分
pointlist = []
for i in [point1, point2, point3, point4]:
if i > 0:
pointlist.append(i)
pointmat = np.array(pointlist)
print(pointlist)
standard_point = np.dot(weightmat, pointmat.T)
print("------>>>标准层得分", standard_point)
return standard_point[0]
point_A = standard_layer_score(A1_A4_weightmat, point_A1, point_A2, point_A3, point_A4)
point_B = standard_layer_score(B1_B3_weightmat, point_B1, point_B2, point_B3)
point_C = standard_layer_score(C1_C2_weightmat, point_C1, point_C2)
point_D = standard_layer_score(D1_D2_weightmat, point_D1, point_D2)
def total_Score(weightmat, point1, point2, point3, point4):
# 计算总得分
pointlist = []
for i in [point1, point2, point3, point4]:
if i > 0:
pointlist.append(i)
pointmat = np.array(pointlist)
print(pointlist)
total_Score = np.dot(weightmat, pointmat.T)
print("------>>>总得分", total_Score[0])
total_Score(A_D_weightmat, point_A, point_B, point_C, point_D)
#如果要用层次分析法问卷调查的数据,用一下程序进行归一化
a1=np.array([0.109,0.176,0.142,0.302,0.270])#评价矩阵分隔开的向量
A3_Emat = np.array([
[0.088, 0.206, 0.234, 0.265, 0.207],
[0.093, 0.129, 0.25, 0.34, 0.188],
[0.007, 0.255, 0.221, 0.341, 0.176]
])
a2=np.array([20,40,60,80,100])
points=np.dot(A3_Emat,a2.T)
print(points)
#归一化处理,归到1-5之内
for point in points:
point_num=point/100
print("%.3f"%float(point_num))