当前位置:   article > 正文

K倍交叉验证配对t检验_k这交叉验证t检验

k这交叉验证t检验

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 − 1 k-1 k1 自由度计算以下 t t t 统计量:
t = p ‾ k ∑ i = 1 k ( 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) 计算第 i i i 次迭代中模型性能之间的差异, p ( i ) = p A ( i ) − p B ( i ) p^{(i)} = p^{(i)}_A - p^{(i)}_B p(i)=pA(i)pB(i) p ‾ \overline{p} p 表示分类器性能之间的平均差异, p ‾ = 1 k ∑ i = 1 k p ( i ) \overline{p} = \frac{1}{k} \sum^k_{i=1} p^{(i)} p=k1i=1kp(i)

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

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

  • 模型性能之间的差异 ( p ( i ) = p A ( i ) − p B ( i ) p^{(i)} = p^{(i)}_A - p^{(i)}_B p(i)=pA(i)pB(i) ) 不是正态分布,因为 p A ( i ) p^{(i)}_A pA(i) p B ( i ) p^{(i)}_B pB(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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
Logistic regression accuracy: 97.37%
Decision tree accuracy: 94.74%
  • 1
  • 2

请注意,由于在重采样过程中产生了新的测试/训练分离,这些精度值不用于配对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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
t statistic: -1.861
p value: 0.096
  • 1
  • 2

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

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

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
Decision tree accuracy: 63.16%
t statistic: 13.491
p value: 0.000
  • 1
  • 2
  • 3

假设我们在显著性水平α=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.}}
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/889538
推荐阅读
相关标签
  

闽ICP备14008679号