sklearn Kfold acces单倍而不是for循环

2024-04-20 08:13:42 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用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 loop):

train_index, test_index in kf[0]

这应该只返回第一个fold,但是我得到了错误:“TypeError:'KFold'对象不支持索引”

我想要的输出:

>>> 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循环的情况下,仅检索一次列车和测试的索引?


Tags: intestforindexnptrainsklearnarray
2条回答
# 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])

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

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

相关问题 更多 >