sklearn Kfold访问单折而不是循环

时间:2014-12-09 13:54:36

标签: python scikit-learn cross-validation

使用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不支持索引"

我想要的输出:

>>> 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循环的情况下检索train和test的索引只进行一次折叠?

2 个答案:

答案 0 :(得分:19)

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

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

kf实际上是一个生成器,它不需要计算列车测试分割。这样可以提高内存使用率,因为您不需要存储不需要的内容。制作KFold对象的列表会强制它使所有值都可用。

以下是两个很好的问题,解释了生成器是什么: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])

答案 1 :(得分:0)

# 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])