[英]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.