相同的测试和预测值为 NER 提供 0 精度、召回率、f1 分数

问题描述

我使用 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 作为其余数据。谁能给我解释一下原因?

需要帮助。提前致谢!

解决方法

您的数据中似乎实际上没有第 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,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,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