赞
踩
比较两个模型性能的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
k−1 自由度计算以下
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/(k−1)
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=k1∑i=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]的假设:
假设我们想要比较两种分类算法,逻辑回归(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 > \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)
Decision tree accuracy: 63.16%
t statistic: 13.491
p value: 0.000
假设我们在显著性水平α=0.05的情况下进行了该测试,我们可以拒绝两个模型在该数据集上表现相同的无效假设,因为p值(p<0.001)小于α。
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.
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/
@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.}}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。