So I tried all the methods above and they are still not quite what I wanted (will explain why).
Step 1: Yes, we need to groupby
the target variable, let's call it target_variable
. So the first part of the code will look like this:
df.groupby('target_variable', group_keys=False)
I am setting group_keys=False
as I am not trying to inherit indexes into the output.
Step2: use apply
to sample from various classes within the target_variable
.
This is where I found the above answers not quite universal. In my example, this is what I have as label numbers in the df
:
array(['S1','S2','normal'], dtype=object),
array([799, 2498,3716391])
So you can see how imbalanced my target_variable
is. What I need to do is make sure I am taking the number of S1
labels as the minimum number of samples for each class.
min(np.unique(df['target_variable'], return_counts=True))
This is what @piRSquared answer is lacking.
Then you want to choose between the min
of the class numbers, 799
here, and the number of each and every class. This is not a general rule and you can take other numbers. For example:
max(len(x), min(np.unique(data_use['snd_class'], return_counts=True)[1])
which will give you the max
of your smallest class compared to the number of each and every class.
The other technical issue in their answer is you are advised to shuffle your output once you have sampled. As in you do not want all S1
samples in consecutive rows then S2
, so forth. You want to make sure your rows are stacked randomly. That is when sample(frac=1)
comes in. The value 1
is because I want to return all the data after shuffling. If you need less for any reason, feel free to provide a fraction like 0.6
which will return 60% of the original sample, shuffled.
Step 3: Final line looks like this for me:
df.groupby('target_variable', group_keys=False).apply(lambda x: x.sample(min(len(x), min(np.unique(df['target_variable'], return_counts=True)[1]))).sample(frac=1))
I am selecting index 1 in np.unique(df['target_variable]. return_counts=True)[1]
as this is appropriate in getting the numbers of each classes as a numpy array
. Feel free to modify as appropriate.