在多标签分类问题中获得每个类别的准确性

Getting accuracy for each category in a multi-label classification problem

我需要计算多标签分类问题中每个类别的准确度(不是整体准确度)。使用 scikit-learn 库中的 classification_report 很容易找到每个类别的精度、召回率和 F 分数。共13个类别分布如下:

                   precision    recall  f1-score   support

     Category 1       0.58      0.48      0.53       244
     Category 2       0.91      0.85      0.88       728
     Category 3       0.90      0.92      0.91      1319
     Category 4       0.70      0.55      0.62       533
     Category 5       1.00      0.10      0.18        20
     Category 6       0.94      0.84      0.89      2038
     Category 7       0.83      0.78      0.80      1930
     Category 8       0.85      0.44      0.58       113
     Category 9       0.88      0.87      0.87      1329
     Category 10      0.79      0.54      0.64        61
     Category 11      0.81      0.77      0.79       562
     Category 12      0.71      0.62      0.66       416
     Category 13      0.76      0.60      0.67       500

      micro avg       0.86      0.78      0.82      9793
      macro avg       0.82      0.64      0.69      9793
   weighted avg       0.85      0.78      0.81      9793
    samples avg       0.86      0.82      0.83      9793

我知道可以按如下方式找到准确度:Accuracy=(TP+TN)/(TP+TN+FP+FN) 但是为这个多标签分类问题找到 TPTN 对我来说是个问题。

在 Whosebug 上有一个与此类似的问题 但仅适用于二元分类问题。

注: 我已经尝试从 sklearn.metrics 中提取 multilabel_confusion_matrixconfusion_matrix 来提取混淆矩阵,但两者都给了我相同的以下错误: ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets

有什么想法吗?

您可以使用以下代码从原始数组手动计算每个 class 的准确度:

class_accuracies = []
for class_ in np.unique(y_true):
    class_acc = np.mean(y_pred[y_true == class_] == class_)
    class_acuracies.append(class_acc)

检查每个标签准确性的公式。在多标签 class 化器中,您可以找到每个标签的准确度 (class) 和每个实例的准确度。