简体   繁体   中英

Applying the KFold Cross Validation on nested dictionary

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?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM