Home > Back-end >  Applying the KFold Cross Validation on nested dictionary
Applying the KFold Cross Validation on nested dictionary

Time:05-09

Input 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}}

Is there any way to apply 2Fold Cross-Validation on this data of nested dictionary? However, this below-mentioned link "https://stackoverflow.com/questions/45115964/separate-pandas-dataframe-using-sklearns-kfold" split the data into train, test. I want to split the data into train, test, and validation?

CodePudding user response:

You can use something like this:

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)

Output:

[0] [1] [2 3]
[1] [0] [2 3]
[0] [1] [0 1]
[1] [0] [0 1]
  • Related