K倍交叉验证配对t检验

比较两个模型性能的K倍配对t检验程序

from mlxtend.evaluate import paired_ttest_kfold_cv

概述

K-fold交叉验证配对t检验程序是比较两个模型(分类器或回归器)性能的常用方法,并解决了重采样t检验程序的一些缺点;然而,这种方法仍然存在训练集重叠的问题,不建议在实践中使用[1],应该使用配对测试5x2cv等技术。

为了解释这种方法是如何工作的,让我们考虑估计量(例如,分类器)A和B。此外,我们有一个标记数据集D。在公共保持方法中,我们通常将数据集分成2个部分:训练和测试集。在k-fold交叉验证配对t检验程序中,我们将测试集分成大小相等的k个部分,然后每个部分用于测试,而剩余的k-1部分(连接在一起)用于训练分类器或回归器(即标准的k-fold交叉验证程序)。

在每个k次交叉验证迭代中,我们计算每个迭代中A和B之间的性能差异,从而获得k个差异度量。现在,通过假设这些k差异是独立绘制的,并且遵循近似正态分布,我们可以根据 Student的t检验(Student’s t test),在模型A和B具有相同性能的无效假设下,用 k−1k-1k−1 自由度计算以下 ttt 统计量:
t=p‾k∑i=1k(p(i)−p‾)2/(k−1).t = \frac{\overline{p} \sqrt{k}}{\sqrt{\sum_{i=1}^{k}(p^{(i) - \overline{p}})^2 / (k-1)}}. t=∑i=1k​(p(i)−p​)2/(k−1)​p​k​​.
这里,p(i)p^{(i)}p(i) 计算第 iii 次迭代中模型性能之间的差异, p(i)=pA(i)−pB(i)p^{(i)} = p^{(i)}_A - p^{(i)}_Bp(i)=pA(i)​−pB(i)​ 和 p‾\overline{p}p​ 表示分类器性能之间的平均差异,p‾=1k∑i=1kp(i)\overline{p} = \frac{1}{k} \sum^k_{i=1} p^{(i)}p​=k1​∑i=1k​p(i)。

一旦我们计算了 ttt 统计量,我们就可以计算 ppp 值,并将其与我们选择的显著性水平进行比较,例如,α=0.05\alpha=0.05α=0.05。如果 ppp 值小于 α\alphaα,我们拒绝零假设,并接受两个模型存在显著差异。

这种方法的问题以及不建议在实践中使用的原因是,它违反了学生t检验(Student’s t test)[1]的假设:

  • 模型性能之间的差异 (p(i)=pA(i)−pB(i)p^{(i)} = p^{(i)}_A - p^{(i)}_Bp(i)=pA(i)​−pB(i)​ ) 不是正态分布,因为 pA(i)p^{(i)}_ApA(i)​ 和 pB(i)p^{(i)}_BpB(i)​ 不是独立的
  • p(i)p^{(i)}p(i) 本身不是独立的,因为训练集重叠

References

  • [1] Dietterich TG (1998) Approximate Statistical Tests for Comparing Supervised Classification Learning Algorithms. Neural Comput 10:1895–1923.

例1-K-折叠交叉验证配对t检验

假设我们想要比较两种分类算法,逻辑回归(logistic regression)和决策树(a decision tree )算法:

from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from mlxtend.data import iris_data
from sklearn.model_selection import train_test_splitX, y = iris_data()
clf1 = LogisticRegression(random_state=1)
clf2 = DecisionTreeClassifier(random_state=1)X_train, X_test, y_train, y_test = \train_test_split(X, y, test_size=0.25,random_state=123)score1 = clf1.fit(X_train, y_train).score(X_test, y_test)
score2 = clf2.fit(X_train, y_train).score(X_test, y_test)print('Logistic regression accuracy: %.2f%%' % (score1*100))
print('Decision tree accuracy: %.2f%%' % (score2*100))
Logistic regression accuracy: 97.37%
Decision tree accuracy: 94.74%

请注意,由于在重采样过程中产生了新的测试/训练分离,这些精度值不用于配对t测试(paired t-test)程序,上述值仅用于直觉。

现在,我们假设显著性阈值α=0.05,以拒绝两种算法在数据集上表现相同的无效假设,并进行k倍交叉验证t检验:

from mlxtend.evaluate import paired_ttest_kfold_cvt, p = paired_ttest_kfold_cv(estimator1=clf1,estimator2=clf2,X=X, y=y,random_seed=1)print('t statistic: %.3f' % t)
print('p value: %.3f' % p)
t statistic: -1.861
p value: 0.096

由于 p>αp > \alphap>α,我们不能拒绝零假设,并且可以得出结论,两种算法的性能没有显著差异。

虽然通常不建议在不纠正多个假设测试的情况下多次应用统计测试,但让我们来看一个示例,其中决策树算法仅限于生成一个非常简单的决策边界,这将导致相对较差的性能:

clf2 = DecisionTreeClassifier(random_state=1, max_depth=1)score2 = clf2.fit(X_train, y_train).score(X_test, y_test)
print('Decision tree accuracy: %.2f%%' % (score2*100))t, p = paired_ttest_kfold_cv(estimator1=clf1,estimator2=clf2,X=X, y=y,random_seed=1)print('t statistic: %.3f' % t)
print('p value: %.3f' % p)
Decision tree accuracy: 63.16%
t statistic: 13.491
p value: 0.000

假设我们在显著性水平α=0.05的情况下进行了该测试,我们可以拒绝两个模型在该数据集上表现相同的无效假设,因为p值(p<0.001)小于α。

API

paired_ttest_kfold_cv(estimator1, estimator2, X, y, cv=10, scoring=None, shuffle=False, random_seed=None)

执行 k-fold 配对t检验( k-fold paired t test )程序来比较两个模型的性能。

Parameters

  • estimator1 : scikit-learn classifier or regressor

  • estimator2 : scikit-learn classifier or regressor

  • X : {array-like, sparse matrix}, shape = [n_samples, n_features]

    Training vectors, where n_samples is the number of samples and n_features is the number of features.

    训练向量,其中 n_samples 是样本数,n_features 是特征数。

  • y : array-like, shape = [n_samples]

    Target values.

  • cv : int (default: 10)

    Number of splits and iteration for the cross-validation procedure

    交叉验证程序的拆分和迭代次数

  • scoring : str, callable, or None (default: None)

    If None (default), uses ‘accuracy’ for sklearn classifiers and ‘r2’ for sklearn regressors. If str, uses a sklearn scoring metric string identifier, for example {accuracy, f1, precision, recall, roc_auc} for classifiers, {‘mean_absolute_error’, ‘mean_squared_error’/‘neg_mean_squared_error’, ‘median_absolute_error’, ‘r2’} for regressors. If a callable object or function is provided, it has to be conform with sklearn’s signature scorer(estimator, X, y); see http://scikit-learn.org/stable/modules/generated/sklearn.metrics.make_scorer.html for more information.

    -如果没有(默认),则对sklearn分类器使用“准确性”,对sklearn回归器使用“r2”。如果str使用sklearn评分度量字符串标识符,例如{accurity,f1,precision,recall,roc_auc}作为分类器,{‘mean_absolute_error’,‘mean_squared_error’/‘neg_mean_squared_error’,‘median_absolute_error’,‘r2’}作为回归器。如果提供了一个可调用的对象或函数,它必须符合sklearn的签名“scorer(estimator,X,y)”;看见http://scikit-learn.org/stable/modules/generated/sklearn.metrics.make_scorer.html了解更多信息。

  • shuffle : bool (default: True)

    Whether to shuffle the dataset for generating the k-fold splits.

    是否洗牌数据集以生成k折叠拆分。

  • random_seed : int or None (default: None)

    Random seed for shuffling the dataset for generating the k-fold splits. Ignored if shuffle=False.

    随机种子,用于对数据集进行洗牌,以生成k折叠拆分。如果shuffle=False,则忽略。

Returns

  • t : float

    The t-statistic

    t 统计量

  • pvalue : float

    Two-tailed p-value. If the chosen significance level is larger than the p-value, we reject the null hypothesis and accept that there are significant differences in the two compared models.

    双尾p值。如果选择的显著性水平大于p值,我们拒绝零假设,并接受两个比较模型存在显著差异。

Examples

For usage examples, please see http://rasbt.github.io/mlxtend/user_guide/evaluate/paired_ttest_kfold_cv/

有关用法示例,请参见http://rasbt.github.io/mlxtend/user_guide/evaluate/paired_ttest_kfold_cv/

reference

@online{Raschka2021Sep,
author = {Raschka, S.},
title = {{K-fold cross-validated paired t test - mlxtend}},
year = {2021},
month = {9},
date = {2021-09-03},
urldate = {2022-03-10},
language = {english},
hyphenation = {english},
note = {[Online; accessed 10. Mar. 2022]},
url = {http://rasbt.github.io/mlxtend/user_guide/evaluate/paired_ttest_kfold_cv},
abstract = {{A library consisting of useful tools and extensions for the day-to-day data science tasks.}}
}

K倍交叉验证配对t检验相关推荐

  1. 在Mnist数据上使用k折交叉验证训练,pytorch代码到底怎么写

    前言 最近学到了K折交叉验证,已经迫不及待去实验一下他的效果是不是如老师讲的一样好,特此写下本文. 本文运行环境为:sklearn.pytorch .jupyter notebook k折交叉验证介绍 ...

  2. k折交叉验证法python实现_Jason Brownlee专栏| 如何解决不平衡分类的k折交叉验证-不平衡分类系列教程(十)...

    作者:Jason Brownlee 编译:Florence Wong – AICUG 本文系AICUG翻译原创,如需转载请联系(微信号:834436689)以获得授权 在对不可见示例进行预测时,模型评 ...

  3. k折交叉验证优缺点_R语言中K邻近算法的初学者指南:从菜鸟到大神(附代码&链接)...

    作者:Leihua Ye, UC Santa Barbara 翻译:陈超 校对:冯羽 本文约2300字,建议阅读10分钟 本文介绍了一种针对初学者的K临近算法在R语言中的实现方法. 本文呈现了一种在R ...

  4. 五折交叉验证/K折交叉验证, python代码到底怎么写

    五折交叉验证: 把数据平均分成5等份,每次实验拿一份做测试,其余用做训练.实验5次求平均值.如上图,第一次实验拿第一份做测试集,其余作为训练集.第二次实验拿第二份做测试集,其余做训练集.依此类推~ 但 ...

  5. Python:K折交叉验证,将数据集分成训练集与测试集

    注意文件夹格式:父文件夹/类别/图像(同torch读取图像格式保存一致),传入路径为父文件夹路径. """ 对图像进行交叉验证, 用于检验分类效果 对每个类别的n张图像进 ...

  6. 【技术分享】什么是K折交叉验证?

    文章目录 1.什么是训练集.验证集和测试集? 2.什么是K折交叉验证? 3.数据集划分过程 3.应用场景及注意事项 3.1.应用场景 3.2.注意事项 1.什么是训练集.验证集和测试集? 训练集,即: ...

  7. 5折交叉验证_[Machine Learning] 模型评估——交叉验证/K折交叉验证

    首先区分两个概念:'模型评估' 与 '模型性能度量' 模型评估:这里强调的是如何划分和利用数据,对模型学习能力的评估,重点在数据的划分方法. Keywords: 划分.利用数据 模型性能度量:是在研究 ...

  8. 交叉验证(cross validation)是什么?K折交叉验证(k-fold crossValidation)是什么?

    交叉验证(cross validation)是什么?K折交叉验证(k-fold crossValidation)是什么? 交叉验证(cross validation)是什么?  交叉验证是一种模型的验 ...

  9. 机器学习(MACHINE LEARNING)交叉验证(简单交叉验证、k折交叉验证、留一法)

    文章目录 1 简单的交叉验证 2 k折交叉验证 k-fold cross validation 3 留一法 leave-one-out cross validation 针对经验风险最小化算法的过拟合 ...

  10. 【Python-ML】SKlearn库Pipeline工作流和K折交叉验证

    # -*- coding: utf-8 -*- ''' Created on 2018年1月18日 @author: Jason.F @summary: Pipeline,流水线工作流,串联模型拟合. ...

最新文章

  1. iconfont 在vue项目中的应用(icon-component组件)
  2. JAVA(利用jsp+javabean+servlet)实现简易计算器
  3. 在java中finalize_在Java垃圾回收中使用finalize()方法
  4. CentOS7 安装 webgoat 7.1 简介
  5. LeetCode 77 组合
  6. [PAL规范]SAP HANA PAL演绎推理算法Apriori编程规范APRIORIRULE
  7. 荣耀30S首发新一代神U麒麟820 5G:GeekBench得分媲美骁龙855
  8. 序号47指标横向展示.xlsx_电力监控系统安全防护规定Akey310参数指标
  9. 为什么你还一直在穷打工?
  10. Linux下查看某个进程占用的CPU及内存
  11. 程序员中的老司机们,30 后的路该开向哪里?
  12. 代码审查(咳咳......又降温了啊....!!!!)
  13. 51单片机实验-蜂鸣器播放音乐
  14. uc/os-II的内存改进与实现TLSF算法的详解,移植实现(四)
  15. 国产划片机 晶圆精密切割机制造商
  16. postfix dovecot邮件服务器搭建
  17. 江南爱窗帘十大品牌 窗帘发展状况怎么样
  18. iOS 局域网通讯 MultipeerConnectivity
  19. 第20节 应用HSRP协议布署双核心交换机网络——提高网络故障容错率
  20. 计算机通过华为手机上网,华为手机网络怎么共享给电脑(必知网络共享3步曲)...

热门文章

  1. C300 OLT自动下发WAN连接指导配置
  2. E4A易安卓Apost提交文本合并编码相关
  3. 微信公众号素材html,微信公众号运营必备:10个免费高清图片素材网站
  4. 流媒体后视镜方案关键技术--调节后视图像显示范围
  5. 还在为日语动词变形感到困惑吗?一张图即可搞定
  6. 大数据处理的基本流程是什么?
  7. A Cleaned, Hypernymed, Image Alt-text Dataset For Automatic Image CaptioningTransformer
  8. 陈龙杰计算机专业,第四届学生职业技能大赛获奖名单
  9. java openoffic linux_怎样使用Java读取OpenOffice文档
  10. C#调用C++类库dll,无法找到函数入口(无法在“***.dll“中找到名为“***“的入口点)