日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

K倍交叉验证配对t检验

發布時間:2023/12/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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個差異度量?,F在,通過假設這些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检验的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。