zoukankan      html  css  js  c++  java
  • 层次分析法AHP

    https://www.cnblogs.com/yhll/p/9967726.html

    https://blog.csdn.net/qq_25594177/article/details/116884419

    import numpy as np
    
    # 计算特征向量和最大特征值
    a = np.array([[1, 1 / 2, 1 / 5],
                  [2, 1, 1 / 2],
                  [5, 2, 1]])  # 建立一个二维数组
    w = np.linalg.eig(a)  # np.linalg.eig(matri)返回特征值和特征向量
    
    print('特征值',w[0])
    print('特征向量',w[1])
    a_max = np.max(w[0])
    print('最大特征值',a_max)
    t = np.argwhere(w[0] == a_max)  # 寻找最大特征值所在的行和列
    print('最大特征值所在的行和列',t[0])
    RILIST = [0, 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]
    n = a.shape[0]
    RI = RILIST[n]
    CI = (a_max - n) / (n - 1)
    CR = CI / RI
    print(CR)
    print("矩阵一致性可接受") if CR < 0.1 else print("矩阵一致性不可接受")
    ans = w[1][::-1, t[0]]
    SUM = sum(ans)
    for i in range(ans.shape[0]):
        ans[i] /= SUM
    print(ans)
    import numpy as np
    
    #计算特征向量和最大特征值
    a=np.array([[1,1/2,1/5],[2,1,1/2],[5,2,1]])   #建立一个二维数组
    w=np.linalg.eig(a)  #np.linalg.eig(matri)返回特征值和特征向量
    a_max=np.max(w[0])
    t=np.argwhere(w[0]==a_max) #寻找最大特征值所在的行和列
    RILIST=[0,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]
    n=a.shape[0]
    RI=RILIST[n]
    CI=(a_max-n)/(n-1)
    CR=CI/RI
    print(CR)
    print("矩阵一致性可接受") if CR<0.1 else print("矩阵一致性不可接受")
    ans=w[1][::-1,t[0]]
    SUM=sum(ans)
    for i in range(ans.shape[0]):
        ans[i]/=SUM
    

    使用类构建一下

    import numpy as np
    
    
    class AHP:
        # 传入的np.ndarray是的判断矩阵
        def __init__(self, array):
            self.array = array
            # 记录矩阵大小
            self.n = array.shape[0]
            # 初始化RI值,用于一致性检验
            RI_list = [0, 0, 0.58, 0.90, 1.12, 1.24, 1.32, 1.41, 1.45]
            self.RI = RI_list[self.n - 1]
    
        # 获取最大特征值和对应的特征向量
        def get_eig(self):
            # numpy.linalg.eig() 计算矩阵特征值与特征向量
            eig_val, eig_vector = np.linalg.eig(self.array)
            # 获取最大特征值
            max_val = np.max(eig_val)
            max_val = round(max_val.real, 4)
            # 通过位置来确定最大特征值对应的特征向量
            index = np.argmax(eig_val)
            max_vector = eig_vector[:, index]
            max_vector = max_vector.real.round(4)
            # 添加最大特征值属性
            self.max_val = max_val
            # 计算权重向量W
            weight_vector = max_vector / sum(max_vector)
            weight_vector = weight_vector.round(4)
            # 打印结果
            print("最大的特征值: " + str(max_val))
            print("对应的特征向量为: " + str(max_vector))
            print("归一化后得到权重向量: " + str(weight_vector))
            return weight_vector
    
        # 测试一致性
        def test_consitst(self):
            # 计算CI值
            CI = (self.max_val - self.n) / (self.n - 1)
            CI = round(CI, 4)
            # 打印结果
            print("判断矩阵的CI值为" + str(CI))
            print("判断矩阵的RI值为" + str(self.RI))
            # 分类讨论
            if self.n == 2:
                print("仅包含两个子因素,不存在一致性问题")
            else:
                # 计算CR值
                CR = CI / self.RI
                CR = round(CR, 4)
                # CR < 0.10才能通过检验
                if CR < 0.10:
                    print("判断矩阵的CR值为" + str(CR) + ",通过一致性检验")
                    return True
                else:
                    print("判断矩阵的CR值为" + str(CR) + ",未通过一致性检验")
                    return False
    
    
    list2 =np.array([[1, 1 / 2, 1 / 5],
                  [2, 1, 1 / 2],
                  [5, 2, 1]])
    a=AHP(list2)
    a.get_eig()
    a.test_consitst()
    

     关于类的实例化参考:

    https://www.cnblogs.com/zmzzm/p/11984489.html

    风雨兼程,前程可待!
  • 相关阅读:
    STM32的备份寄存器测试
    dsp6657的helloworld例程测试-第一篇
    Dennis Gabor与全息摄影
    Gabor filter与Gabor transform
    图像生成器:让电脑学习生成数字图像
    Haar-like feature和Haar wavelet
    《Wonderland: A Novel Abstraction-Based Out-Of-Core Graph Processing System》章明星
    小波分析及其应用
    Discrete cosine transform(离散余弦转换)
    psimpl_v7_win32_demo
  • 原文地址:https://www.cnblogs.com/xingnie/p/14851563.html
Copyright © 2011-2022 走看看