<jupyter_start><jupyter_text>---------------------------------------- Implementation of k-fold cross-validation- For tabular datasets- For image-like datasets- For mixed datasets Because the datasets in this project are so small, reserving test-data could hurt results more than it improves reporting accuracy. Relying on cross-validation allows all data to be shown to model for training. Requires a reporting of max, min, and average results----------------------------------------  Text dataset and cv<jupyter_code>kf = KFold(n_splits = 5)

for fold, (train_index, test_index) in enumerate(kf.split(x_train, y_train)):
    print(fold)
    X_trn = x_train.iloc[train_index]
    X_tst = x_train.iloc[test_index]
    
    pce_trn = y_train['PCE'].iloc[train_index]
    pce_tst = y_train['PCE'].iloc[test_index]
    voc_trn = y_train['VocL'].iloc[train_index]
    voc_tst = y_train['VocL'].iloc[test_index]
    jsc_trn = y_train['Jsc'].iloc[train_index]
    jsc_tst = y_train['Jsc'].iloc[test_index]
    ff_trn = y_train['FF'].iloc[train_index]
    ff_tst = y_train['FF'].iloc[test_index]
    
    X_trn = torch.tensor(X_trn.values.astype(np.float32))
    X_tst = torch.tensor(X_tst.values.astype(np.float32))
    pce_trn = torch.tensor(pce_trn.values.astype(np.float32))
    pce_tst = torch.tensor(pce_tst.values.astype(np.float32))
    voc_trn = torch.tensor(voc_trn.values.astype(np.float32))
    voc_tst = torch.tensor(voc_tst.values.astype(np.float32))
    jsc_trn = torch.tensor(jsc_trn.values.astype(np.float32))
    jsc_tst = torch.tensor(jsc_tst.values.astype(np.float32))
    ff_trn = torch.tensor(ff_trn.values.astype(np.float32))
    ff_tst = torch.tensor(ff_tst.values.astype(np.float32))
    
    train_dataset = torch.utils.data.TensorDataset(X_trn, pce_trn, voc_trn, jsc_trn, ff_trn)
    test_dataset = torch.utils.data.TensorDataset(X_tst, pce_tst, voc_tst, jsc_tst, ff_tst)
    
    train_loader = torch.utils.data.DataLoader(dataset = train_dataset)
    test_loader = torch.utils.data.DataLoader(dataset = test_dataset)
def get_fold_dataloaders_df(x_train, y_train, train_index, test_index):
    X_trn = x_train.iloc[train_index]
    X_tst = x_train.iloc[test_index]
    
    pce_trn = y_train['PCE'].iloc[train_index]
    pce_tst = y_train['PCE'].iloc[test_index]
    voc_trn = y_train['VocL'].iloc[train_index]
    voc_tst = y_train['VocL'].iloc[test_index]
    jsc_trn = y_train['Jsc'].iloc[train_index]
    jsc_tst = y_train['Jsc'].iloc[test_index]
    ff_trn = y_train['FF'].iloc[train_index]
    ff_tst = y_train['FF'].iloc[test_index]
    
    X_trn = torch.tensor(X_trn.values.astype(np.float32))
    X_tst = torch.tensor(X_tst.values.astype(np.float32))
    pce_trn = torch.tensor(pce_trn.values.astype(np.float32))
    pce_tst = torch.tensor(pce_tst.values.astype(np.float32))
    voc_trn = torch.tensor(voc_trn.values.astype(np.float32))
    voc_tst = torch.tensor(voc_tst.values.astype(np.float32))
    jsc_trn = torch.tensor(jsc_trn.values.astype(np.float32))
    jsc_tst = torch.tensor(jsc_tst.values.astype(np.float32))
    ff_trn = torch.tensor(ff_trn.values.astype(np.float32))
    ff_tst = torch.tensor(ff_tst.values.astype(np.float32))
    
    train_dataset = torch.utils.data.TensorDataset(X_trn, pce_trn, voc_trn, jsc_trn, ff_trn)
    test_dataset = torch.utils.data.TensorDataset(X_tst, pce_tst, voc_tst, jsc_tst, ff_tst)
    
    train_loader = torch.utils.data.DataLoader(dataset = train_dataset)
    test_loader = torch.utils.data.DataLoader(dataset = test_dataset)
    
    return train_loader, test_loader
kf = KFold(n_splits = 5)

for fold, (train_index, test_index) in enumerate(kf.split(x_train, y_train)):
    print(fold)
    
    train_loader, test_loader = get_fold_dataloaders_df(x_train, y_train, train_index, test_index)
    
    print(train_loader)<jupyter_output>0
<torch.utils.data.dataloader.DataLoader object at 0x1a2faaca50>
1
<torch.utils.data.dataloader.DataLoader object at 0x1a301d6bd0>
2
<torch.utils.data.dataloader.DataLoader object at 0x1a301d2d10>
3
<torch.utils.data.dataloader.DataLoader object at 0x1a301d65d0>
4
<torch.utils.data.dataloader.DataLoader object at 0x1a301d2390><jupyter_text>Image dataset and cv<jupyter_code>def get_fold_dataloaders_im_s3(im_dict, label_df, train_index, test_index):
    x_trn = im_dict[train_index]
    pce_trn = label_df['PCE'].iloc[train_index]
    voc_trn = label_df['VocL'].iloc[train_index]
    jsc_trn = label_df['Jsc'].iloc[train_index]
    ff_trn = label_df['FF'].iloc[train_index]

    x_tst = im_dict[test_index]
    pce_tst = label_df['PCE'].iloc[test_index]
    voc_tst = label_df['VocL'].iloc[test_index]
    jsc_tst = label_df['Jsc'].iloc[test_index]
    ff_tst = label_df['FF'].iloc[test_index]

    x_trn = torch.tensor(x_trn.values.astype(np.float32))
    x_tst = torch.tensor(x_tst.values.astype(np.float32))
    pce_trn = torch.tensor(pce_trn.values.astype(np.float32))
    pce_tst = torch.tensor(pce_tst.values.astype(np.float32))
    voc_trn = torch.tensor(voc_trn.values.astype(np.float32))
    voc_tst = torch.tensor(voc_tst.values.astype(np.float32))
    jsc_trn = torch.tensor(jsc_trn.values.astype(np.float32))
    jsc_tst = torch.tensor(jsc_tst.values.astype(np.float32))
    ff_trn = torch.tensor(ff_trn.values.astype(np.float32))
    ff_tst = torch.tensor(ff_tst.values.astype(np.float32))

    train_dataset = torch.utils.data.TensorDataset(x_trn, pce_trn, voc_trn, jsc_trn, ff_trn)
    test_dataset = torch.utils.data.TensorDataset(x_tst, pce_tst, voc_tst, jsc_tst, ff_tst)

    train_loader = torch.utils.data.DataLoader(dataset = train_dataset)
    test_loader = torch.utils.data.DataLoader(dataset = test_dataset)

    return train_loader, test_loader
im_dict, label_df = nuts.load_s3_ims(bucket_name, filepath)

kf = KFold(n_splits = 5)

for fold, (train_index, test_index) in enumerate(kf.split(im_dict, label_df)):

    train_loader, test_loader = get_fold_dataloaders_im_s3(im_dict, label_df, train_index, test_index)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13518024032042725, 'fraction_numerical': 0.018024032042723633, 'mean_word_length': 4.493125572868927, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '168954', 'license': 'Apache-2.0', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2474, 'n_words': 324, 'path': 'ipynb/Data/Cross-validation_dataset.ipynb', 'repo_name': 'wesleyktatum/py-conjugated', 'size': '5992'}