maxframe.learn.metrics.multilabel_confusion_matrix#

maxframe.learn.metrics.multilabel_confusion_matrix(y_true, y_pred, *, sample_weight=None, labels=None, samplewise=False, execute=False, session=None, run_kwargs=None)[源代码]#

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

按类别(默认)或按样本(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} 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_weight (array-like of shape (n_samples,), default=None) -- 样本权重。

  • labels (array-like of shape (n_classes,), default=None) -- 一个类别或列索引列表,用于选择某些类别(或强制包含数据中缺失的类别)。

  • samplewise (bool, default=False) -- 在多标签情况下,这将为每个样本计算一个混淆矩阵。

返回:

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

返回类型:

ndarray of shape (n_outputs, 2, 2)

参见

confusion_matrix

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

备注

multilabel_confusion_matrix 按类别或按样本计算多标签混淆矩阵,在多类任务中,标签采用一对一方式进行二值化;而 confusion_matrix() 则计算每两个类别之间的单一混淆矩阵。

示例

多类情况:

>>> import maxframe.tensor as mt
>>> from maxframe.learn.metrics import multilabel_confusion_matrix
>>> 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"]).execute()
array([[[3, 1],
        [0, 2]],

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

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

多标签指示符情况尚未实现。