导入xlsx数据做熵权topsis综合评价模型代码简单(有正向指标和负向指标)(输出结果包括到正负理想解的距离、权重、得分)
时间: 2024-12-23 14:20:25 浏览: 7
导入Excel数据并应用Entropy权重TOPSIS(Technique for Order Preference by Similarity to Ideal Solution)综合评价模型的Python代码通常涉及pandas库处理数据,numpy计算矩阵操作,sklearn库用于熵权计算,以及matplotlib绘制结果。这里是一个简化的示例:
```python
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import math
# 假设你已经有了一个名为data.xlsx的DataFrame,其中包含正向指标列(如A列)和负向指标列(如B列)
df = pd.read_excel('data.xlsx')
# 步骤1:数据预处理
scaler = MinMaxScaler()
df_scaled = scaler.fit_transform(df)
# 步骤2:确定指标和权重
indicators = df.columns.tolist()
positive_indicators = [i for i in indicators if 'positive' in i] # 假设正向指标前缀是'positive'
negative_indicators = [i for i in indicators if 'negative' in i]
weights = calculate_entropy_weights(positive_indicators, negative_indicators) # 使用熵法计算权重
# 步骤3:计算正向理想解和负向理想解
ideal_solution = {'pos': np.max(df_scaled, axis=0), 'neg': np.min(df_scaled, axis=0)}
# 步骤4:计算距离
dist_pos = ideal_solution['pos'] - df_scaled
dist_neg = df_scaled - ideal_solution['neg']
# 步骤5:TOPSIS得分
score = (np.sum(dist_pos * weights, axis=1) / np.linalg.norm(dist_pos, axis=1)) + \
(-np.sum(dist_neg * weights, axis=1) / np.linalg.norm(dist_neg, axis=1))
# 输出结果
print(f"每个决策变量的得分: {score}")
print(f"距离正向理想解: {dist_pos}")
print(f"距离负向理想解: {dist_neg}")
print(f"每个指标的权重: {weights}")
def calculate_entropy_weights(pos_indicators, neg_indicators):
total_weight = sum(np.ones(len(indicators)))
pos_weight = np.array([math.log(len(df[df[col].notnull()])) for col in pos_indicators])
neg_weight = np.array([math.log(len(df[df[col].isnull()])) for col in neg_indicators])
entropy_pos = -(pos_weight / total_weight).sum() * total_weight
entropy_neg = -(neg_weight / total_weight).sum() * total_weight
return (total_weight - entropy_pos - entropy_neg) / total_weight
#
阅读全文