Python新手求助:如何在Friedman-Nemenyi检验中添加CD限值
问题:Friedman-Nemenyi检验图添加CD限值
我是Python新手,已在Jupyter Notebook中完成不同机器学习回归模型的对比,并运行了Friedman-Nemenyi检验。现有代码可实现检验,但不知如何正确添加CD限值,希望生成带CD限值的检验图(与目标图样式一致),恳请帮忙查看代码并指导。
当前代码:
# This method computes the critical difference for Nemenyi test cd = compute_CD(average_ranks, n=len(df), alpha='0.05', test='nemenyi') # This method generates the plot. graph_ranks(average_ranks, names=algorithms_names, cd=2.8, width=10, textspace=1.5, reverse=False) plt.show()
解决方案
- 核心问题是你手动固定了
cd=2.8,应该直接传入计算好的cd变量,这样才是匹配你数据集的正确临界差值。修改后的代码:
# 计算Nemenyi检验的临界差值(CD) cd = compute_CD(average_ranks, n=len(df), alpha='0.05', test='nemenyi') # 生成带CD限值的排名图,使用计算得到的cd值 graph_ranks(average_ranks, names=algorithms_names, cd=cd, width=10, textspace=1.5, reverse=False) plt.show()
- 确认依赖库:
compute_CD和graph_ranks属于scikit-posthocs库,若未安装,先在Notebook中执行:
!pip install scikit-posthocs
- 校验输入参数:确保
average_ranks是模型的平均排名数组,algorithms_names是对应模型的名称列表,两者顺序必须严格对应,否则图中标签会错位。 - 样式调整:如果生成图与目标样式不符,可调整
width(图宽)、textspace(标签间距)参数,或设置reverse=True反转排名顺序(若目标图是从高到低排序)。
内容的提问来源于stack exchange,提问作者Dani1991
相关产品推荐
相关产品推荐

