繁体   English   中英

Select 只有在多类分类问题中具有最佳度量(f1 分数)的类

[英]Select only classes with best metric (f1 score) in a multiclass classification problem

我有近 50 个类的多类分类问题。 在我运行模型后,一些课程获得了很好的分数(0.70 或更高),而其他课程则表现不佳。

我想做的是根据我获得的指标,只保留成绩好的课程,并只为他们创建一个 model

如何从我的 model 的分类报告结果中挑选出好的类?

这是我要提取并保留的类

在此处输入图像描述

classification_report有一个output_dict参数,它导致 function 返回字典而不是字符串。

如果你有一个好的f1 分数的阈值(例如0.7 ),你可以迭代结果和 select 值高于阈值的标签:

from sklearn.metrics import classification_report

y_true = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]
y_pred = [0, 1, 2, 0, 0, 1, 4, 3, 1, 1, 2, 2, 2, 3, 2, 1, 3, 3, 3]
labels = [0, 1, 2, 3]

cr = classification_report(y_true, y_pred, output_dict=True)

for l in labels:
    if (f1_score := cr[str(l)]["f1-score"]) > 0.7:
        print(f"Label {l}, f1-score: {f1_score:.3f}")

Output:

Label 0, f1-score: 0.750
Label 2, f1-score: 0.800

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM