multilabel_confusion_matrix#

sklearn.metrics.multilabel_confusion_matrix(y_true, y_pred, *, sample_weight=None, labels=None, samplewise=False)#

计算每个类别或样本的混淆矩阵。

Added in version 0.21.

计算类别的(默认)或样本的(samplewise=True)多标签混淆矩阵,以评估分类的准确性,并输出每个类别或样本的混淆矩阵。

在多标签混淆矩阵 \(MCM\) 中,真阴性的计数是 \(MCM_{:,0,0}\) ,假阴性是 \(MCM_{:,1,0}\) ,真阳性是 \(MCM_{:,1,1}\) ,假阳性是 \(MCM_{:,0,1}\)

多类数据将被视为在一对多转换下二值化处理。返回的混淆矩阵将按照 (y_true, y_pred) 联合中排序的唯一标签的顺序排列。

更多信息请参阅 用户指南

Parameters:
y_true{array-like, sparse matrix} of shape (n_samples, n_outputs) or (n_samples,)

真实目标值(正确的)。

y_pred{array-like, sparse matrix} of shape (n_samples, n_outputs) or (n_samples,)

分类器返回的估计目标。

sample_weightarray-like of shape (n_samples,), default=None

样本权重。

labelsarray-like of shape (n_classes,), default=None

一个类别列表或列索引,用于选择某些类别(或强制包含数据中不存在的类别)。

samplewisebool, default=False

在多标签情况下,这会计算每个样本的混淆矩阵。

Returns:
multi_confusionndarray of shape (n_outputs, 2, 2)

对应于输入中每个输出的 2x2 混淆矩阵。当计算类别的 multi_confusion(默认)时,n_outputs = n_labels;当计算样本的 multi_confusion(samplewise=True)时,n_outputs = n_samples。如果定义了 labels ,结果将按照 labels 中指定的顺序返回,否则结果将默认按排序顺序返回。

See also

confusion_matrix

计算混淆矩阵以评估分类器的准确性。

Notes

multilabel_confusion_matrix 计算类别或样本的多标签混淆矩阵,并且在多类任务中,标签在一对多的方式下二值化处理;而 confusion_matrix 计算每两个类别之间的混淆矩阵。

Examples

多标签指示器情况:

>>> import numpy as np
>>> from sklearn.metrics import multilabel_confusion_matrix
>>> y_true = np.array([[1, 0, 1],
...                    [0, 1, 0]])
>>> y_pred = np.array([[1, 0, 0],
...                    [0, 1, 1]])
>>> multilabel_confusion_matrix(y_true, y_pred)
array([[[1, 0],
        [0, 1]],

       [[1, 0],
        [0, 1]],

       [[0, 1],
        [1, 0]]])

多类情况:

>>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
>>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
>>> multilabel_confusion_matrix(y_true, y_pred,
...                             labels=["ant", "bird", "cat"])
array([[[3, 1],
        [0, 2]],

       [[5, 0],
        [1, 0]],

       [[2, 1],
        [1, 2]]])