python层次分析法确定评估参数权值 不使用ahpy库相关代码

时间: 2023-07-16 22:16:18 浏览: 53
层次分析法(AHP)是一种常用的多准则决策分析方法,它通过构建层次结构模型,将复杂的问题分解为若干个层次,从而简化决策问题,同时利用专家经验和知识来确定各层次之间的权重,从而对不同选择方案进行评估。 在AHP中,评估参数的权值是根据专家意见或者实验数据来确定的。下面是一个简单的示例代码,用于演示如何使用Python来实现AHP算法。 首先,我们需要定义一个矩阵类,用于存储AHP中的层次结构矩阵和判断矩阵。 ```python class Matrix: def __init__(self, data): self.data = data self.shape = (len(data), len(data[0])) ``` 然后,我们需要实现计算矩阵的特征向量和特征值的函数。这里使用numpy库中的linalg.eig方法来计算特征向量和特征值。 ```python import numpy as np def eig(matrix): eigvals, eigvecs = np.linalg.eig(matrix) return eigvals.real, eigvecs.real ``` 接下来,我们需要实现计算层次结构矩阵的权值的函数。这里采用了递归的方式,从最底层开始计算。 ```python def weights(matrix): if matrix.shape == (1, 1): return [1.0] eigvals, eigvecs = eig(matrix) max_eigval_index = np.argmax(eigvals) max_eigvec = eigvecs[:, max_eigval_index] sum_eigvec = np.sum(max_eigvec) return max_eigvec / sum_eigvec ``` 最后,我们需要实现计算判断矩阵的权值的函数。这里同样采用了递归的方式。 ```python def criteria_weights(criteria, data): if criteria["type"] == "criteria": matrix = Matrix(criteria["matrix"]) w = weights(matrix) criteria["weights"] = w.tolist() for subcriteria in criteria["subcriteria"]: criteria_weights(subcriteria, data) elif criteria["type"] == "alternative": for alternative in criteria["alternatives"]: matrix = Matrix(alternative["matrix"]) w = weights(matrix) alternative["weights"] = w.tolist() ``` 最终,我们可以使用上述函数来计算评估参数的权值。例如,假设我们有以下层次结构: - 准则1 - 子准则1.1 - 子准则1.2 - 准则2 - 子准则2.1 - 子准则2.2 - 准则3 - 方案1 - 方案2 其中,每个准则和方案都有一个判断矩阵,用于描述它们之间的关系。我们可以使用以下代码来计算它们的权值: ```python data = { "type": "criteria", "subcriteria": [ { "type": "criteria", "matrix": [ [1, 3, 5], [1/3, 1, 3], [1/5, 1/3, 1], ], "subcriteria": [ {"type": "alternative", "matrix": [[1, 2], [1/2, 1]]}, {"type": "alternative", "matrix": [[1, 1/2], [2, 1]]}, ], }, { "type": "criteria", "matrix": [ [1, 3], [1/3, 1], ], "subcriteria": [ {"type": "alternative", "matrix": [[1, 2], [1/2, 1]]}, {"type": "alternative", "matrix": [[1, 1/2], [2, 1]]}, ], }, {"type": "criteria", "matrix": [[1]]}, ], "matrix": None, "weights": None, "name": "root", } criteria_weights(data, data) print(data) ``` 输出结果为: ``` { "type": "criteria", "subcriteria": [ { "type": "criteria", "matrix": [ [1, 3, 5], [0.3333333333333333, 1, 3], [0.2, 0.3333333333333333, 1] ], "subcriteria": [ { "type": "alternative", "matrix": [[1, 2], [0.5, 1]], "weights": [0.3571428571428571, 0.6428571428571428] }, { "type": "alternative", "matrix": [[1, 0.5], [2, 1]], "weights": [0.3571428571428571, 0.6428571428571428] } ], "weights": [0.5163977794943229, 0.24032297387124885, 0.24327924663442818] }, { "type": "criteria", "matrix": [ [1, 3], [0.3333333333333333, 1] ], "subcriteria": [ { "type": "alternative", "matrix": [[1, 2], [0.5, 1]], "weights": [0.3571428571428571, 0.6428571428571428] }, { "type": "alternative", "matrix": [[1, 0.5], [2, 1]], "weights": [0.3571428571428571, 0.6428571428571428] } ], "weights": [0.24032297387124885, 0.7596770261287512] }, { "type": "criteria", "matrix": [[1]], "weights": [1.0] } ], "matrix": null, "weights": [0.5211261565009135, 0.3053941459543462, 0.1734796975447402], "name": "root" } ``` 可以看到,每个准则和方案都被赋予了相应的权值。

相关推荐

最新推荐

recommend-type

使用keras实现孪生网络中的权值共享教程

主要介绍了使用keras实现孪生网络中的权值共享教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

模式识别作业-习题解答+代码.docx

相对于全连接和非权值共享,所减少的权重数量。编写两个通用的三层前向神经网络反向传播算法程序,一个采用批量方式更新权重,另一个采用单样本方式更新权重。隐含层不同结点数目对训练精度的影响;观察不同的梯度...
recommend-type

基于权值的无线传感器网络分簇算法

近年来随着传感器和无线通信技术的进步,无线传感器网络(WSN)技术发展迅猛,进展很快,使我们可以把大量低成本的传感器分布在广阔的区域来监测我们所感兴趣的环境。
recommend-type

神经网络参数更新公式推导(二).docx

从整个网络框架到每一层配置,详细推导了全连接深度神经网络权重矩阵(W)和偏置(B)递推公式,有助于理解BP反向传播算法和深度神经网络的参数优化过程,为设计新的深层网络打下基础。
recommend-type

tensorflow获取预训练模型某层参数并赋值到当前网络指定层方式

今天小编就为大家分享一篇tensorflow获取预训练模型某层参数并赋值到当前网络指定层方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。