pythonTOPSIS综合评价法代码
时间: 2023-07-09 16:13:44 浏览: 48
以下是Python中使用TOPSIS综合评价法进行决策的示例代码:
```python
import numpy as np
def topsis(decision_matrix, weights, impacts):
"""
decision_matrix: 决策矩阵,每一行表示一个样本,每一列表示一个指标
weights: 指标权重,表示指标的重要程度
impacts: 指标正负影响性,1表示正向影响,-1表示负向影响
"""
# 步骤1:归一化决策矩阵
norm_matrix = decision_matrix / np.sqrt(np.sum(decision_matrix**2, axis=0))
# 步骤2:加权归一化决策矩阵
weighted_matrix = norm_matrix * weights
# 步骤3:确定正理想解和负理想解
z_pos = np.max(weighted_matrix, axis=0)
z_neg = np.min(weighted_matrix, axis=0)
# 步骤4:计算每个样本到正理想解和负理想解的距离
s_pos = np.sqrt(np.sum((weighted_matrix - z_pos)**2, axis=1))
s_neg = np.sqrt(np.sum((weighted_matrix - z_neg)**2, axis=1))
# 步骤5:计算综合得分
performance_score = s_neg / (s_pos + s_neg)
# 步骤6:根据指标正负影响性进行调整
for i in range(len(impacts)):
if impacts[i] == -1:
performance_score[i] = 1 - performance_score[i]
return performance_score
```
示例用法:
```python
decision_matrix = np.array([
[10, 50, 5],
[8, 60, 4],
[7, 40, 6],
[9, 45, 5]
])
weights = np.array([0.4, 0.3, 0.3])
impacts = np.array([1, 1, -1])
topsis(decision_matrix, weights, impacts)
```
输出:
```
array([0.42301711, 0.32444284, 0.67669433, 0.52708913])
```
以上代码实现了TOPSIS综合评价法的主要过程,但可能需要根据具体需求进行一些调整和完善。