mcnemar_table:McNemar检验的列联表
计算麦克纳马尔检验的2x2列联表的函数
> `from mlxtend.evaluate import mcnemar_table`
概述
McNemar检验的列联表
一个 2x2 列联表在麦克尼马尔检验中使用(mlxtend.evaluate.mcnemar
),是比较两个不同模型的有用工具。与典型的混淆矩阵不同,这个表比较两个模型之间的关系,而不是显示单个模型预测的假阳性、真阳性、假阴性和真阴性:
例如,考虑到两个模型的准确率分别为99.7%和99.6%,一个2x2的列联表可以为模型选择提供进一步的见解。
在子图A和B中,这两个模型的预测准确率如下:
- 模型1准确率:9,960 / 10,000 = 99.6%
- 模型2准确率:9,970 / 10,000 = 99.7%
现在,在子图A中,我们可以看到模型2正确预测了11个模型1错误的结果。反之,模型2错误预测了1个模型2正确的结果。因此,基于这个11:1的比例,我们可以得出结论,模型2的表现显著优于模型1。然而,在子图B中,比例为25:15,这对选择哪个模型更好,结论就不那么明确了。
参考文献
- McNemar, Quinn, 1947. "关于相关比例或百分比差异的抽样误差的说明". Psychometrika. 12 (2): 153–157.
- Edwards AL: 关于检验相关比例差异显著性时“连续性修正”的说明。Psychometrika. 1948, 13 (3): 185-187. 10.1007/BF02289261.
- https://zh.wikipedia.org/wiki/McNemar%E8%AF%95%E9%AA%8C
示例 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()
API
mcnemar_table(y_target, y_model1, y_model2)
Compute a 2x2 contingency table for McNemar's test.
Parameters
-
y_target
: array-like, shape=[n_samples]True class labels as 1D NumPy array.
-
y_model1
: array-like, shape=[n_samples]Predicted class labels from model as 1D NumPy array.
-
y_model2
: array-like, shape=[n_samples]Predicted class labels from model 2 as 1D NumPy array.
Returns
-
tb
: array-like, shape=[2, 2]2x2 contingency table with the following contents: a: tb[0, 0]: # of samples that both models predicted correctly b: tb[0, 1]: # of samples that model 1 got right and model 2 got wrong c: tb[1, 0]: # of samples that model 2 got right and model 1 got wrong d: tb[1, 1]: # of samples that both models predicted incorrectly
Examples
For usage examples, please see https://rasbt.github.io/mlxtend/user_guide/evaluate/mcnemar_table/