[英]Same test and prediction values gives 0 precision, recall, f1 score for NER
我使用 sklearns crfsuite 來計算 f1、精度和召回分數,但出現異常。 出於測試目的,我給出了相同的測試和預測值。
from sklearn_crfsuite import scorers
from sklearn_crfsuite import metrics
cls = [i for i, _ in enumerate(CLASSES)]
cls.append(7)
cls.append(8)
print(metrics.flat_classification_report(
test["y"], test["y"], labels=cls, digits=3
))
precision recall f1-score support
0 1.000 1.000 1.000 551
1 0.000 0.000 0.000 0
2 0.000 0.000 0.000 0
3 1.000 1.000 1.000 1196
4 1.000 1.000 1.000 2593
5 1.000 1.000 1.000 95200
6 1.000 1.000 1.000 1165
7 1.000 1.000 1.000 9636
8 1.000 1.000 1.000 506363
micro avg 1.000 1.000 1.000 616704
macro avg 0.778 0.778 0.778 616704
weighted avg 1.000 1.000 1.000 616704
為什么 1 和 2 個標簽都給出 0 分。 它應該給出 1 作為數據的 rest。 誰能給我解釋一下原因?
需要幫忙。 提前致謝!
似乎您的數據中實際上沒有類 1 和 2,因為這兩個類的支持為零,但是由於您在傳遞給flat_classification_report()
的標簽列表中包含了類 1 和 2,因此它們仍然被考慮在各種指標的計算。
from sklearn_crfsuite import metrics
import numpy as np
np.random.seed(0)
cmin = 0
cmax = 8
labels = np.arange(1 + cmax)
print(np.unique(labels))
# [0 1 2 3 4 5 6 7 8]
y = np.random.randint(cmin, 1 + cmax, 1000).reshape(-1, 1)
print(np.unique(y))
# [0 1 2 3 4 5 6 7 8]
# classification report when "y" takes on all the specified labels
print(metrics.flat_classification_report(y_true=y, y_pred=y, labels=labels, digits=3))
# precision recall f1-score support
# 0 1.000 1.000 1.000 117
# 1 1.000 1.000 1.000 106
# 2 1.000 1.000 1.000 106
# 3 1.000 1.000 1.000 132
# 4 1.000 1.000 1.000 110
# 5 1.000 1.000 1.000 115
# 6 1.000 1.000 1.000 104
# 7 1.000 1.000 1.000 109
# 8 1.000 1.000 1.000 101
# accuracy 1.000 1000
# macro avg 1.000 1.000 1.000 1000
# weighted avg 1.000 1.000 1.000 1000
# classification report when "y" takes on all the specified labels apart from 1 and 2,
# but 1 and 2 are still included among the possible labels
y = y[np.logical_and(y != 1, y != 2)].reshape(-1, 1)
print(np.unique(y))
# [0 3 4 5 6 7 8]
print(metrics.flat_classification_report(y_true=y, y_pred=y, labels=labels, digits=3))
# precision recall f1-score support
# 0 1.000 1.000 1.000 117
# 1 0.000 0.000 0.000 0
# 2 0.000 0.000 0.000 0
# 3 1.000 1.000 1.000 132
# 4 1.000 1.000 1.000 110
# 5 1.000 1.000 1.000 115
# 6 1.000 1.000 1.000 104
# 7 1.000 1.000 1.000 109
# 8 1.000 1.000 1.000 101
# micro avg 1.000 1.000 1.000 788
# macro avg 0.778 0.778 0.778 788
# weighted avg 1.000 1.000 1.000 788
# classification report when "y" takes on all the specified labels apart from 1 and 2,
# and 1 and 2 are not included among the possible labels
labels = labels[np.logical_and(labels != 1, labels != 2)]
print(np.unique(labels))
# [0 3 4 5 6 7 8]
print(metrics.flat_classification_report(y_true=y, y_pred=y, labels=labels, digits=3))
# precision recall f1-score support
# 0 1.000 1.000 1.000 117
# 3 1.000 1.000 1.000 132
# 4 1.000 1.000 1.000 110
# 5 1.000 1.000 1.000 115
# 6 1.000 1.000 1.000 104
# 7 1.000 1.000 1.000 109
# 8 1.000 1.000 1.000 101
# accuracy 1.000 788
# macro avg 1.000 1.000 1.000 788
# weighted avg 1.000 1.000 1.000 788
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.