【问题标题】:sklearn Kfold acces single fold instead of for loopsklearn Kfold 访问单折而不是 for 循环
【发布时间】:2015-02-07 10:40:51
【问题描述】:

使用 cross_validation.KFold(n, n_folds=folds) 后,我想访问索引以训练和测试单折,而不是遍历所有折。

那么让我们以示例代码为例:

from sklearn import cross_validation
X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
y = np.array([1, 2, 3, 4])
kf = cross_validation.KFold(4, n_folds=2)

>>> print(kf)  
sklearn.cross_validation.KFold(n=4, n_folds=2, shuffle=False,
                           random_state=None)
>>> for train_index, test_index in kf:

我想像这样访问 kf 中的第一个折叠(而不是 for 循环):

train_index, test_index in kf[0]

这应该只返回第一个折叠,但我得到了错误:“TypeError: 'KFold' object does not support indexing”

我想要的输出:

>>> train_index, test_index in kf[0]
>>> print("TRAIN:", train_index, "TEST:", test_index)
TRAIN: [2 3] TEST: [0 1]

链接:http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.KFold.html

问题

如何在不遍历整个 for 循环的情况下仅检索一次训练和测试的索引?

【问题讨论】:

    标签: python scikit-learn cross-validation


    【解决方案1】:
    # We saved all the K Fold samples in different list  then we access to this throught [i]
    from sklearn.model_selection import KFold
    import numpy as np
    import pandas as pd
    
    kf = KFold(n_splits=4)
    
    X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
    
    Y = np.array([0,0,0,1])
    Y=Y.reshape(4,1)
    
    X=pd.DataFrame(X)
    Y=pd.DataFrame(Y)
    
    
    X_train_base=[]
    X_test_base=[]
    Y_train_base=[]
    Y_test_base=[]
    
    for train_index, test_index in kf.split(X):
    
        X_train, X_test = X.iloc[train_index,:], X.iloc[test_index,:]
        Y_train, Y_test = Y.iloc[train_index,:], Y.iloc[test_index,:]
        X_train_base.append(X_train)
        X_test_base.append(X_test)
        Y_train_base.append(Y_train)
        Y_test_base.append(Y_test)
    
    print(X_train_base[0])
    print(Y_train_base[0])
    print(X_train_base[1])
    print(Y_train_base[1])
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。您可以使用edit 按钮改进此答案以获得更多选票和声誉!
    【解决方案2】:

    你在正确的轨道上。您现在需要做的就是:

    kf = cross_validation.KFold(4, n_folds=2)
    mylist = list(kf)
    train, test = mylist[0]
    

    kf 实际上是一个生成器,它在需要之前不会计算训练测试拆分。这可以提高内存使用率,因为您不会存储不需要的项目。列出KFold 对象会强制它使所有值都可用。

    这里有两个很好的 SO 问题来解释什么是生成器:onetwo


    2018 年 11 月编辑

    自 sklearn 0.20 以来,API 发生了变化。更新示例(针对 py3.6):

    from sklearn.model_selection import KFold
    import numpy as np
    
    kf = KFold(n_splits=4)
    
    X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
    
    
    X_train, X_test = next(kf.split(X))
    
    In [12]: X_train
    Out[12]: array([2, 3])
    
    In [13]: X_test
    Out[13]: array([0, 1])
    

    【讨论】:

    • 这确实有效,谢谢:) 但我猜最后一行代码应该是:train, test = l[0]?
    • 不错的答案,但您实际上并不需要实现所有折叠:只需 train, test = next(kf) 就可以了。
    • 使用next(kf) 返回“KFold 对象不是迭代器”
    • @mbatchkarov 看起来sklearn 发生了重大变化。 KFold(4, n_folds=2) 现在抛出 TypeError: __init__() got an unexpected keyword argument 'n_folds'
    • 重命名为 n_splits
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2011-10-06
    • 2017-10-18
    • 2021-11-01
    相关资源
    最近更新 更多