我有两个不同来源的数据集 . 我将它们合并并制作最终数据集 . 我正在应用k-fold交叉验证,并希望确保每个折叠的列车测试分割应该来自两个来源的内容条目 . 以下是示例代码:
from sklearn.utils import shuffle
from sklearn.model_selection import KFold, StratifiedKFold
import numpy as np
datasource1=np.array([1,2,3,4,5,6,7,8,9,10,11])
datasource1_label=np.array([0,1,0,1,0,1,0,1,0,1,0])#0 for odd and 1 for even
datasource2=np.array([12,13,14,15])
datasource2_label=np.array([1,0,1,0])#0 for odd and 1 for even
merge_data=np.concatenate((datasource1,datasource2))
merge_label=np.concatenate((datasource1_label, datasource2_label))
x,y = shuffle(merge_data,merge_label)
kf = KFold(n_splits=3,shuffle=False)
kf.get_n_splits()
train_index_list=[];test_index_list=[]
for train_index, test_index in kf.split(x,y):
x_train, x_test = x[train_index], x[test_index]
y_train, y_test = y[train_index], y[test_index]
train_index_list.append(train_index)
test_index_list.append(test_index)
我没有提及random_state用于shuffle和kfold,因为我尝试了不同的实验 . n_split cab可以是5倍或10倍的任何值 . 有时可能会发生任何一个(或多个)折叠不满足第二个数据集或测试数据类似的任何值 .