TOPSISpython代码
时间: 2023-11-08 12:04:32 浏览: 21
您可以使用Python来实现TOPSIS算法。以下是一个示例代码:
```python
import numpy as np
def topsis(dataset, weights, impacts):
# 标准化数据集
normalized_matrix = dataset / np.sqrt(np.sum(dataset ** 2, axis=0))
# 加权标准化后的矩阵
weighted_matrix = normalized_matrix * weights
# 理想解和负理想解
ideal_best = np.max(weighted_matrix, axis=0)
ideal_worst = np.min(weighted_matrix, axis=0)
# 计算正理想解和负理想解之间的欧氏距离
positive_distance = np.sqrt(np.sum((weighted_matrix - ideal_best) ** 2, axis=1))
negative_distance = np.sqrt(np.sum((weighted_matrix - ideal_worst) ** 2, axis=1))
# 性能得分
performance_score = negative_distance / (positive_distance + negative_distance)
# 综合评价指数
composite_index = 1 - performance_score
return composite_index
# 示例数据集
dataset = np.array([[5, 3, 4, 4],
[3, 1, 2, 3],
[4, 3, 4, 3],
[3, 3, 1, 5]])
# 权重向量
weights = np.array([0.25, 0.25, 0.25, 0.25])
# 影响向量
impacts = np.array([1, 1, -1, 1])
# 使用TOPSIS算法计算综合评价指数
result = topsis(dataset, weights, impacts)
print(result)
```