mcnemar_table: McNemar's 检验的列联表

计算 McNemar's 检验的 2x2 列联表的函数

from mlxtend.evaluate import mcnemar_table

概述

McNemar's 检验的列联表

McNemar's 检验 (mlxtend.evaluate.mcnemar) 中使用的 2x2 列联表是比较两个不同模型的有用辅助工具。与典型的混淆矩阵不同,该表比较的是两个模型本身,而不是展示单个模型预测的假阳性、真阳性、假阴性和真阴性结果。

例如,假设两个模型的准确度分别为 99.7% 和 99.6%,2x2 列联表可以为模型选择提供进一步的见解。

在子图 A 和 B 中,两个模型的预测准确度如下:

  • 模型 1 准确度: 9,960 / 10,000 = 99.6%
  • 模型 2 准确度: 9,970 / 10,000 = 99.7%

现在,在子图 A 中,我们可以看到模型 2 正确预测了模型 1 预测错误的 11 个样本。反之,模型 1 正确预测了模型 2 预测错误的 1 个样本。因此,基于这个 11:1 的比例,我们可以得出结论:模型 2 的性能明显优于模型 1。然而,在子图 B 中,比例是 25:15,这对于选择哪个模型更好没有那么明确的结论。

参考文献

示例 2 - 2x2 列联表

import numpy as np
from mlxtend.evaluate import mcnemar_table

y_true = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])

y_mod1 = np.array([0, 1, 0, 0, 0, 1, 1, 0, 0, 0])
y_mod2 = np.array([0, 0, 1, 1, 0, 1, 1, 0, 0, 0])

tb = mcnemar_table(y_target=y_true, 
                   y_model1=y_mod1, 
                   y_model2=y_mod2)

tb
array([[4, 1],
       [2, 3]])

为了通过 matplotlib 可视化(并更好地解释)列联表,我们可以使用 checkerboard_plot 函数

from mlxtend.plotting import checkerboard_plot
import matplotlib.pyplot as plt

brd = checkerboard_plot(tb,
                        figsize=(3, 3),
                        fmt='%d',
                        col_labels=['model 2 wrong', 'model 2 right'],
                        row_labels=['model 1 wrong', 'model 1 right'])
plt.show()

png

API

mcnemar_table(y_target, y_model1, y_model2)

计算 McNemar's 检验的 2x2 列联表。

参数

  • y_target : 类似数组,形状=[n_samples]

    真实类别标签,为一维 NumPy 数组。

  • y_model1 : 类似数组,形状=[n_samples]

    模型 1 预测的类别标签,为一维 NumPy 数组。

  • y_model2 : 类似数组,形状=[n_samples]

    模型 2 预测的类别标签,为一维 NumPy 数组。

返回

  • tb : 类似数组,形状=[2, 2]

    2x2 列联表,包含以下内容: a: tb[0, 0]: 两个模型都预测正确的样本数 b: tb[0, 1]: 模型 1 预测正确但模型 2 预测错误的样本数 c: tb[1, 0]: 模型 2 预测正确但模型 1 预测错误的样本数 d: tb[1, 1]: 两个模型都预测错误的样本数

示例

有关用法示例,请参阅 https://mlxtend.cn/mlxtend/user_guide/evaluate/mcnemar_table/