当多个指标相互制约、权重难以主观确定时,熵权法(EWM)客观赋权 + 模糊综合评价(FCE)科学打分,是学术界公认的高效方案。本文提供一份开箱即用的Python代码,一键完成计算、导出Excel、生成顶刊风格图表。一、为什么选择 EWM-FCE
在多指标综合评价中,我们经常面临两个难题:
权重怎么定? 主观赋权(如AHP)容易受专家偏好影响;客观赋权(如熵权法)基于数据本身离散程度,更公正。
得分怎么算? 简单加权和忽略了指标间量纲差异,而模糊综合评价通过隶属度函数将不同指标统一到同一尺度。
熵权法 + 模糊综合评价(EWM-FCE) 的流程是:
该方法已广泛应用于环境质量评价、农业试验优选、项目风险评估等领域。
二、代码功能一览
下面这份完整的Python代码实现:
读取Excel数据(处理组合 + 多个指标)
正向化处理(将逆向指标转为正向)
标准化(极差归一化)
熵权法计算:指标比重矩阵 → 信息熵 → 熵权权重
模糊综合评价:线性加权计算每个处理组合的综合得分
导出6张Excel工作表:原始数据、正向化数据、标准化数据、比重矩阵、权重结果、最终排名
生成4张专业图表:
指标权重排序图(粉色渐变)
处理组合得分排名图(森林绿渐变)
T×W交互热力图(双色渐变)
Top3组合雷达图(粉色渐变)
代码自动处理全0列、除零警告、文件被占用等异常,并输出高分辨率图片(600 dpi),满足期刊投稿要求。
三、完整代码与逐段解读
🧩 0. 导入库与路径设置
import osimport timeimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsfrom matplotlib.colors import LinearSegmentedColormapINPUT_PATH = r"E:\DataAnalysis\Data.xlsx"SAVE_DIR = r"E:\DataAnalysis\EWM-FCE"os.makedirs(SAVE_DIR, exist_ok=True)os.makedirs(os.path.join(SAVE_DIR, "plots"), exist_ok=True)
🎨 1. 自定义配色方案
# 粉色系渐变(薄雾粉→晶石紫)pink_palette = ["#F9EDF4", "#EDC9DD", "#E0A5C7", "#D180B1", "#C15B9B", "#B02F86"]custom_pink_cmap = LinearSegmentedColormap.from_list("custom_pink", pink_palette, N=256)# 森林绿单色渐变(森林绿→晨雾白绿)green_palette = ["#D4E7CF", "#B8D8B0", "#9CC891", "#7FB873", "#61A855"]custom_green_cmap = LinearSegmentedColormap.from_list("custom_green", green_palette, N=256)# 双色渐变(森林绿→浆果红)dual_palette = ["#61A855", "#7FB873", "#9CC891", "#B8D8B0", "#D4E7CF", "#FFD6D4", "#FFBAB8", "#FE9E9D", "#F98082", "#F36069"]custom_dual_cmap = LinearSegmentedColormap.from_list("custom_dual", dual_palette, N=256)
⚙️ 2. 全局绘图风格
plt.rcParams.update({ "font.family": ["Arial", "SimHei"], "font.size": 10, "axes.titlesize": 12, "axes.labelsize": 11, "xtick.labelsize": 9, "ytick.labelsize": 9, "legend.fontsize": 9, "figure.dpi": 300, "savefig.dpi": 600, "savefig.bbox": "tight", "axes.spines.top": False, "axes.spines.right": False, "grid.alpha": 0.3, "grid.linestyle": "--", "axes.unicode_minus": False})
中英文混排支持
高分辨率输出
简洁现代的风格(无顶部/右侧边框,虚线网格)
📥 3. 数据读取与预处理
df = pd.read_excel(INPUT_PATH, sheet_name="Sheet1")df_mean = df.groupby(["Treatment", "Factor1", "Factor2"]).mean().reset_index()group_cols = ["Treatment", "Factor1", "Factor2"]indicators = [col for col in df_mean.columns if col not in group_cols]
Excel包含:
先按三个分组变量求均值(若有重复测量),然后自动识别指标列。
🔧 4. 正向化与标准化(核心函数)
def data_preprocessing(df_mean, indicators, is_positive=None): if is_positive is None: is_positive = {ind: True for ind in indicators} data_pos = df_mean[indicators].copy() for ind in indicators: if not is_positive[ind]: data_pos[ind] = data_pos[ind].max() - data_pos[ind] data_norm = pd.DataFrame() for ind in indicators: col_min = data_pos[ind].min() col_max = data_pos[ind].max() if col_max == col_min: data_norm[ind] = 0.5 # 全列相同值,统一设为0.5,避免全0 else: data_norm[ind] = (data_pos[ind] - col_min) / (col_max - col_min + 1e-8) return data_pos, data_norm
📐 5. 熵权法计算权重
def ewm_calculate_weights(data_norm): col_sum = data_norm.sum(axis=0) col_sum[col_sum == 0] = 1e-8 p_matrix = data_norm / col_sum n_samples = data_norm.shape[0] k = 1 / np.log(n_samples + 1e-8) entropy = -k * (p_matrix * np.log(p_matrix + 1e-8)).sum(axis=0) weights = (1 - entropy) / ((1 - entropy).sum() + 1e-8) return p_matrix, entropy, weights
比重矩阵p_matrix:每个样本在该指标上的占比。
信息熵entropy:熵值越小,指标变异程度越大,权重应越高。
熵权weights:(1 - 熵) / 总和(1-熵)。
所有分母加 1e-8 避免除零,np.log(0) 通过 +1e-8 规避。
🧮 6. 模糊综合评价(线性加权)
def fce_linear_weight(data_norm, weights): fce_scores = np.dot(data_norm, weights.T) return fce_scoresfce_scores = fce_linear_weight(data_norm, weights)result_df = df_mean[group_cols].copy()result_df["综合得分"] = np.round(fce_scores, 4)result_df["排名"] = result_df["综合得分"].rank(ascending=False, method="min").astype(int)
综合得分 = 标准化数据 × 权重向量
按得分降序排名
📎 7. 导出Excel(6张工作表,含容错处理)
timestamp = time.strftime("%Y%m%d_%H%M%S")excel_path = os.path.join(SAVE_DIR, f"EWM-FCE_计算全过程_{timestamp}.xlsx")try: with pd.ExcelWriter(excel_path, engine="openpyxl") as writer: df_mean.to_excel(writer, sheet_name="1_原始均值数据", index=False) data_pos_with_group = df_mean[group_cols].join(data_pos) data_pos_with_group.to_excel(writer, sheet_name="2_正向化数据", index=False) data_norm_with_group = df_mean[group_cols].join(data_norm) data_norm_with_group.to_excel(writer, sheet_name="3_标准化数据", index=False) p_with_group = df_mean[group_cols].join(pd.DataFrame(p_matrix, columns=indicators)) p_with_group.to_excel(writer, sheet_name="4_指标比重矩阵", index=False) weight_df = pd.DataFrame({ "指标名称": indicators, "信息熵": np.round(entropy, 4), "熵权权重": np.round(weights, 4), "权重占比(%)": np.round(weights * 100, 2) }).sort_values("熵权权重", ascending=False) weight_df.to_excel(writer, sheet_name="5_指标权重结果", index=False) result_df_sorted = result_df.sort_values("排名").reset_index(drop=True) result_df_sorted.to_excel(writer, sheet_name="6_综合评价排名", index=False) print(f"✅ Excel文件已保存:{excel_path}")except PermissionError: # 如果文件被打开,自动保存备份 backup_excel_path = os.path.join(SAVE_DIR, f"EWM-FCE_计算全过程_BACKUP_{timestamp}.xlsx") # ... 同样写入操作 print(f"⚠️ 已自动保存到备用文件:{backup_excel_path}")
📊 8. 生成4张专业图表
图1:指标权重排序柱状图(粉色渐变)
plt.figure(figsize=(12, 6))sorted_weight = weight_df.sort_values("熵权权重")colors = custom_pink_cmap(np.linspace(0, 1, len(sorted_weight)))plt.barh(sorted_weight["指标名称"], sorted_weight["熵权权重"], color=colors, edgecolor="white")plt.xlabel("熵权权重", fontweight="bold")plt.ylabel("指标名称", fontweight="bold")plt.title("各测量指标熵权权重排序", fontweight="bold")plt.grid(axis="x")plt.savefig(os.path.join(SAVE_DIR, "plots", "01_指标权重排序图.png"))
横向柱状图,权重从低到高排列
粉色渐变色,直观展示哪些指标对评价贡献最大
图2:处理组合综合得分排名图(森林绿渐变)
plt.figure(figsize=(10, 7))sorted_result = result_df_sortedcolors = custom_green_cmap(np.linspace(0, 1, len(sorted_result)))plt.barh(sorted_result["Treatment"][::-1], sorted_result["综合得分"][::-1], color=colors[::-1])plt.xlabel("模糊综合得分(越高越优)", fontweight="bold")plt.ylabel("处理组合(T×W)", fontweight="bold")plt.title("处理组合综合得分排名", fontweight="bold")plt.grid(axis="x")plt.savefig(os.path.join(SAVE_DIR, "plots", "02_组合得分排名图.png"))
图3:T×W交互热力图(双色渐变:森林绿→浆果红)
plt.figure(figsize=(8, 6))heat_data = result_df.pivot_table(index="Factor1", columns="Factor2", values="综合得分", aggfunc="mean")sns.heatmap(heat_data, annot=True, cmap=custom_dual_cmap, fmt=".4f", linewidths=0.8, linecolor="white", cbar_kws={"label": "模糊综合得分"})plt.title("T×W处理组合综合得分热力图", fontweight="bold")plt.xlabel("Factor2 (W)", fontweight="bold")plt.ylabel("Factor1 (T)", fontweight="bold")plt.savefig(os.path.join(SAVE_DIR, "plots", "03_TW交互热力图.png"))
图4:Top3组合雷达图(粉色渐变)
plt.figure(figsize=(8, 8))top3_combinations = result_df_sorted.head(3)["Treatment"].tolist()plot_indicators = indicators[:8] # 雷达图指标过多会拥挤,取前8个n_plot = len(plot_indicators)angles = np.linspace(0, 2 * np.pi, n_plot, endpoint=False).tolist()angles += angles[:1]ax = plt.subplot(111, polar=True)for i, treat in enumerate(top3_combinations): treat_data = data_norm[df_mean["Treatment"] == treat][plot_indicators].values[0].tolist() treat_data += treat_data[:1] color = custom_pink_cmap(i / 3) ax.plot(angles, treat_data, label=f"{treat}(排名{i+1})", color=color, linewidth=2) ax.fill(angles, treat_data, color=color, alpha=0.2)ax.set_xticks(angles[:-1])ax.set_xticklabels(plot_indicators, fontsize=9)plt.title("Top3处理组合指标得分雷达图", fontweight="bold", pad=20)plt.legend(loc="upper right", bbox_to_anchor=(1.2, 1.0))plt.savefig(os.path.join(SAVE_DIR, "plots", "04_Top3组合雷达图.png"))
极坐标展示前三名在各指标上的标准化得分
面积越大、形状越饱满,综合表现越好
适合比较不同方案的优劣势
📢 9. 控制台输出
print("="*60)print("✅ 熵权-模糊综合评价分析完成!")print(f"📂 所有文件保存路径:{SAVE_DIR}")print("="*60)best_result = result_df_sorted.iloc[0]print(f"🏆 最优处理组合:{best_result['Treatment']}")print(f"📊 综合得分:{best_result['综合得分']}")print(f"🔝 权重Top3指标:{weight_df.head(3)['指标名称'].tolist()}")print("="*60)print("🖼️ 生成图表列表:")print(" 1. 01_指标权重排序图.png")print(" 2. 02_组合得分排名图.png")print(" 3. 03_TW交互热力图.png")print(" 4. 04_Top3组合雷达图.png")print("="*60)
四、如何修改代码适配自己的数据?
修改文件路径
INPUT_PATH = r"你的Excel文件路径"
指定正向/逆向指标
默认所有指标均为正向(越大越好)。若有逆向指标,在调用data_preprocessing时传入字典:
is_positive = {"成本": False, "缺陷率": False}data_pos, data_norm = data_preprocessing(df_mean, indicators, is_positive)
3. 调整雷达图显示的指标数量
如果指标数量超过8个,雷达图会过于拥挤,可以修改plot_indicators = indicators[:8]为其他切片或手动选择。
4. 修改配色
直接替换pink_palette、green_palette、dual_palette中的颜色码。
五、总结
📌 环境要求
Python 3.8+
依赖库:pandas, numpy, matplotlib, seaborn, openpyxl
一键安装:pip install pandas numpy matplotlib seaborn openpyxl
💡 小提示:Excel文件中请确保第一行为列名,数值列不含文本或合并单元格。
赶快用你的代码和数据试试吧! 🚀
如果你觉得有帮助的话,欢迎点赞、收藏、转发!有任何问题或改进建议,也欢迎在评论区交流。