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