multilabel_confusion_matrix#

sklearn.metrics.multilabel_confusion_matrix(y_true, y_pred, *, sample_weight=None, labels=None, samplewise=False)[源代码]#

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

0.21 版本新增。

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

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

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

用户指南 中阅读更多内容。

参数:
y_true{array-like, sparse matrix},形状为 (n_samples, n_outputs) 或 (n_samples,)

地面真实(正确)目标值。仅当标签为 多标签 类型时才支持稀疏矩阵。

y_pred{array-like, sparse matrix},形状为 (n_samples, n_outputs) 或 (n_samples,)

估计目标值,如分类器返回。仅当标签为 多标签 类型时才支持稀疏矩阵。

sample_weightshape 为 (n_samples,) 的 array-like, default=None

样本权重。

labels形状为 (n_classes,) 的类数组对象, 默认为 None

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

samplewisebool,默认=False

在多标签情况下,此项会为每个样本计算一个混淆矩阵。

返回:
multi_confusionndarray,形状为 (n_outputs, 2, 2)

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

另请参阅

confusion_matrix

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

注意事项

multilabel_confusion_matrix 计算类别-wise 或样本-wise 的多标签混淆矩阵,在多类任务中,标签通过一对多方式进行二值化;而 confusion_matrix 计算一个混淆矩阵来衡量任意两个类别之间的混淆。

示例

多标签指示器情况

>>> 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]]])