mcnemar_table:McNemar检验的列联表

计算麦克纳马尔检验的2x2列联表的函数

> `from mlxtend.evaluate import mcnemar_table`

概述

McNemar检验的列联表

一个 2x2 列联表在麦克尼马尔检验中使用(mlxtend.evaluate.mcnemar),是比较两个不同模型的有用工具。与典型的混淆矩阵不同,这个表比较两个模型之间的关系,而不是显示单个模型预测的假阳性、真阳性、假阴性和真阴性:

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

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

现在,在子图A中,我们可以看到模型2正确预测了11个模型1错误的结果。反之,模型2错误预测了1个模型2正确的结果。因此,基于这个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)

Compute a 2x2 contingency table for McNemar's test.

Parameters

Returns

Examples

For usage examples, please see https://rasbt.github.io/mlxtend/user_guide/evaluate/mcnemar_table/