You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python循环中如何配对打印模型名称与Specificity函数计算结果

问题原因

你使用了嵌套的两层循环,会导致每个模型名遍历全部3个混淆矩阵,所以出现重复输出、模型和计算结果不匹配的问题。

修复后代码

arrays = [qda_confusion_matrix, Knn_5_confusion_matrix, Tree_Boo_confusion_matrix]
names = ['QDA', '5-NN', 'Tree con Boosting']

# 用zip将两个列表相同位置的元素配对遍历
for name, array in zip(names, arrays):
    print(name)
    Specificity(array)

补充说明

如果你自定义的Specificity函数本身不会打印结果,仅返回数值的话,可以调整循环内的代码,手动打印结果:

for name, array in zip(names, arrays):
    print(name)
    spe_res = Specificity(array)
    print(f"Specificity: {spe_res:.3f}\n")

内容的提问来源于stack exchange,提问作者Marco Andres Alarcon Sierra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 15:36:02