K倍交叉验证配对t检验

本文介绍了K折交叉验证配对t检验在比较两个模型性能时的应用,如逻辑回归与决策树。通过K折交叉验证,计算t统计量和p值来判断模型间是否存在显著差异。当p值大于显著性水平(如0.05)时,无法拒绝零假设,即模型间无显著差异。示例中展示了在不同设置下,两种模型的性能差异可能达到显著性水平,从而影响决策。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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-1k1 自由度计算以下 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/(k1)pk.
这里,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=k1i=1kp(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_split


X, 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_cv


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)
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.}}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

或许,这就是梦想吧!

如果对你有用,欢迎打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值