Java数据集拆分,将数据集拆分为从两个不同数据源获取的训练和测试,并确保拆分数据集包含来自两个源的条目...

我有两个不同来源的数据集 . 我将它们合并并制作最终数据集 . 我正在应用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倍的任何值 . 有时可能会发生任何一个(或多个)折叠不满足第二个数据集或测试数据类似的任何值 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值