【问题标题】:What is the difference between a "normal" k-fold cross-validation using shuffle=True and a repeated k-fold cross-validation?使用 shuffle=True 的“正常”k 折交叉验证和重复的 k 折交叉验证有什么区别?
【发布时间】:2021-04-06 07:55:01
【问题描述】:

谁能解释使用 shuffle 函数的“正常”k-fold 交叉验证之间的区别,例如

kf = KFold(n_splits = 5, shuffle = True)

还有重复的 k 折交叉验证?他们不应该返回相同的结果吗?

很难理解其中的区别。

感谢任何提示。

【问题讨论】:

    标签: python scikit-learn cross-validation shuffle k-fold


    【解决方案1】:

    顾名思义,RepeatedKFold 是重复的KFold。 它执行n_repeats 次。当n_repeats=1 时,前者的性能与shuffle=True 时的后者完全相同。 它们不会返回相同的拆分,因为默认情况下是 random_state=None,也就是说,您没有指定它。 因此,他们使用不同的种子来(伪)随机打乱数据。

    当它们具有相同的random_state 并重复一次时,它们都会导致相同的拆分。要更深入地了解,请尝试以下操作:

    import pandas as pd
    from sklearn.model_selection import KFold, RepeatedKFold
                         
    data = pd.DataFrame([['red', 'strawberry'], # color, fruit
                      ['red', 'strawberry'], 
                      ['red', 'strawberry'],
                      ['red', 'strawberry'],
                      ['red', 'strawberry'],
                      ['yellow', 'banana'],
                      ['yellow', 'banana'],
                      ['yellow', 'banana'],
                      ['yellow', 'banana'],
                      ['yellow', 'banana']])
    
    X = data[0]
    
    # KFold
    for train_index, test_index in KFold(n_splits=2, shuffle=True, random_state=1).split(X):
        print("TRAIN:", train_index, "TEST:", test_index)
    
    # RepeatedKFold
    for train_index, test_index in RepeatedKFold(n_splits=2, n_repeats=1, random_state=1).split(X):
        print("TRAIN:", train_index, "TEST:", test_index)
    

    您应该获得以下内容:

    TRAIN: [1 3 5 7 8] TEST: [0 2 4 6 9]
    TRAIN: [0 2 4 6 9] TEST: [1 3 5 7 8]
    
    TRAIN: [1 3 5 7 8] TEST: [0 2 4 6 9]
    TRAIN: [0 2 4 6 9] TEST: [1 3 5 7 8]
    

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 2020-08-29
      • 1970-01-01
      • 2016-09-30
      • 2018-08-29
      • 2017-06-09
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多