I have a training dataset that is composed numpy array of examples X_train, and pandas dataframe labels Y_train. The dataframe contains three columns: The number of the class, the name of the class, and the participant in the experiment from which the example was measured. I want to randomize 3 participants, and extract a single example from each of them for a specific class. So far, I have managed to extract the indices, though I am sure my way is unnecessarily complicated.
Activities = ['eating'] * 60
Activities.extend(['sleeping'] * 60)
participants = np.repeat(np.arange(30)+1, 4).tolist()
Y_train = pd.DataFrame({'Activity_Name':Activities ,'partisipant': participants})
def plot_for_activity(act):
# A sub dataframe with the intended activity
sub_df = Y_train[Y_train['Activity_Name']==act]
# Find indices of 3 different participents:
Num_of_participents = sub_df['partisipant'].nunique()
Ids = np.random.choice(sub_df['partisipant'].unique(), 3,replace=False)
idx = list()
for id in Ids:
idx.extend(sub_df.loc[sub_df['partisipant']==id].sample(n=1).index.tolist())
return idx
print(plot_for_activity('eating'))
print(plot_for_activity('sleeping'))
For example, I have data from 30 participants about sleeping and eating. Eventually, I want to present the data for sleeping, from two different random participants and the data from eating from two different random participants. The function should accept the activity name, and based on that, extract the data from two different random participants.
Can anyone offer a code that is more compact and clean for this?