我試圖在我的多標簽分類問題中計算每類的精度、召回率和 F1 分數。但是,我認為我做錯了什么,因為我得到了非常高的值,整個問題的 F1 分數是 0.66。但是,我在各個班級中獲得了 0.8 f1-score。
這就是我現在的做法:
confusion_matrix = multilabel_confusion_matrix(gold_labels, predictions)
assert(len(confusion_matrix) == 6)
for label in range(len(labels_reduced)):
tp = confusion_matrix[label][0][0]
fp = confusion_matrix[label][0][1]
fn = confusion_matrix[label][1][0]
tn = confusion_matrix[label][1][1]
precision = tp fp
precision = tp/precision
recall = tp fn
recall = tp/recall
f1_score_up = precision * recall
f1_score_down = precision recall
f1_score = f1_score_up/f1_score_down
f1_score = 2 * f1_score
print(f"Metrics for {labels_reduced[label]}.")
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1-Score: {f1_score}")
這些結果好嗎?它們有意義嗎?難道我做錯了什么?您將如何計算這些指標?我正在使用 huggingface 轉換器來加載模型并獲得預測,并使用 sklearn 來計算指標。
uj5u.com熱心網友回復:
您可以使用以下classification_report功能sklearn:
from sklearn.metrics import classification_report
labels = [[0, 1, 1], [1, 0, 0], [1, 0, 1]]
predictions = [[[0, 0, 1], [1, 0, 0], [1, 1, 1]]
report = classification_report(labels, predictions)
print(report)
哪個輸出:
precision recall f1-score support
0 1.00 1.00 1.00 2
1 0.00 0.00 0.00 1
2 1.00 1.00 1.00 2
micro avg 0.80 0.80 0.80 5
macro avg 0.67 0.67 0.67 5
weighted avg 0.80 0.80 0.80 5
samples avg 0.89 0.83 0.82 5
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/528375.html
