MCPcopy Create free account
hub / github.com/DrugLT/ImDrug / create_group_split

Function create_group_split

lib/utils/split.py:819–841  ·  view source on GitHub ↗

split within each stratification defined by the group column for training/validation split Args: train_val (pd.DataFrame): the train+valid dataframe to split on seed (int): the random seed holdout_frac (float): the fraction of validation group_column (str): the name of the gro

(train_val, seed, holdout_frac, group_column)

Source from the content-addressed store, hash-verified

817
818
819def create_group_split(train_val, seed, holdout_frac, group_column):
820 """split within each stratification defined by the group column for training/validation split
821
822 Args:
823 train_val (pd.DataFrame): the train+valid dataframe to split on
824 seed (int): the random seed
825 holdout_frac (float): the fraction of validation
826 group_column (str): the name of the group column
827
828 Returns:
829 dict: a dictionary of splitted dataframes, where keys are train/valid/test and values correspond to each dataframe
830 """
831 train_df = pd.DataFrame()
832 val_df = pd.DataFrame()
833
834 for i in train_val[group_column].unique():
835 train_val_temp = train_val[train_val[group_column] == i]
836 np.random.seed(seed)
837 msk = np.random.rand(len(train_val_temp)) < (1 - holdout_frac)
838 train_df = train_df.append(train_val_temp[msk])
839 val_df = val_df.append(train_val_temp[~msk])
840
841 return {'train': train_df.reset_index(drop = True), 'valid': val_df.reset_index(drop = True)}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected