topsis的python代码
时间: 2023-07-23 20:07:48 浏览: 257
以下是使用Python实现的TOPSIS算法的代码示例:
```python
import numpy as np
def topsis(data, weights, impacts):
# 步骤1:归一化矩阵
normalized_data = data / np.sqrt(np.sum(data**2, axis=0))
# 步骤2:加权归一化矩阵
weighted_normalized_data = normalized_data * weights
# 步骤3:计算正理想解和负理想解
positive_ideal = np.max(weighted_normalized_data, axis=1)
negative_ideal = np.min(weighted_normalized_data, axis=1)
# 步骤4:计算正负理想解之间的距离
positive_distances = np.sqrt(np.sum((weighted_normalized_data - positive_ideal)**2, axis=1))
negative_distances = np.sqrt(np.sum((weighted_normalized_data - negative_ideal)**2, axis=1))
# 步骤5:计算接近度指数
performance_scores = negative_distances / (positive_distances + negative_distances)
# 根据正负影响进行调整
for i in range(len(impacts)):
if impacts[i] == '-':
performance_scores[i] = 1 / (performance_scores[i] + 0.000001) # 避免除零错误
elif impacts[i] == '+':
performance_scores[i] = performance_scores[i]
return performance_scores
# 示例数据
data = np.array([[250, 16, 12],
[200, 12, 8],
[300, 20, 16],
[275, 18, 14]])
weights = np.array([0.4, 0.3, 0.3])
impacts = ['-','+','+']
# 使用TOPSIS算法计算指标的绩效得分
scores = topsis(data, weights, impacts)
print(scores)
```
请注意,上述代码中的示例数据仅为演示目的,您可以根据您自己的数据进行相应的调整。
阅读全文