在嵌套字典上应用 KFold 交叉验证

Applying the KFold Cross Validation on nested dictionary

输入字典

new_dict1 = {'ABW':{'ABR':1,'BPR':1,'CBR':1,'DBR':0},'BCW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0},
        'CBW':{'ABR':1,'BPR':1,'CBR':0,'DBR':0},'MCW':{'ABR':1,'BPR':1,'CBR':0,'DBR':1},
        'DBW':{'ABR':0,'BPR':0,'CBR':1,'DBR':0}}

有没有办法对嵌套字典的数据应用2Fold交叉验证?然而,下面提到的这个 link “ 将数据拆分为训练、测试。我想将数据拆分为训练、测试和验证?

你可以这样使用:

from sklearn.model_selection import KFold
df = pd.DataFrame(new_dict1)
kf = KFold(n_splits = 2, shuffle = True, random_state = 0)
inds = kf.split(df)
for train_val_index, test_index in inds:
    kf = KFold(n_splits = 2, shuffle = True, random_state = 0)
    inds2 = kf.split(train_val_index)
    for train_index, val_index in inds2:
        print(train_index, val_index, test_index)

输出:

[0] [1] [2 3]
[1] [0] [2 3]
[0] [1] [0 1]
[1] [0] [0 1]