Reputation: 1193
I have 2 classes, A and B. Each class has an unbalanced number of samples to each other, say 500 to class A and 1000 to class B.
Is there a way to extract balanced number of samples for each class, say 300 for class A and B using scikit learn or any of the Numpy functions.
The samples are the first 5 columns and the labels/classes is the final column
1 2 3 4 5 1
2 3 4 2 3 1
4 0 5 4 3 1
4 5 9 2 4 2
5 9 5 3 9 2
What I would like to be done is have an equal amount of both classes in my final pick:
2 3 4 2 3 1
4 0 5 4 3 1
4 5 9 2 4 2
5 9 5 3 9 2
Upvotes: 4
Views: 2021
Reputation: 210832
You can use sample() method with the same value for n
parameter, if you can use Pandas
Demo:
In [364]: df1
Out[364]:
a b c d
0 0.774496 0.852985 0.257568 0.223773
1 0.630460 0.203675 0.305280 0.965628
2 0.408746 0.939827 0.801505 0.343216
3 0.578582 0.541716 0.451810 0.173890
4 0.210301 0.600485 0.184326 0.035092
5 0.583564 0.164262 0.958537 0.943357
In [365]: df2
Out[365]:
a b c d
0 0.340624 0.755825 0.569149 0.543630
1 0.004056 0.463891 0.556861 0.778607
2 0.171046 0.293104 0.317514 0.831424
3 0.370028 0.566356 0.895919 0.440559
4 0.148569 0.485086 0.299789 0.274720
5 0.137273 0.085598 0.874845 0.917356
6 0.356898 0.781540 0.091851 0.173430
7 0.495949 0.613337 0.512104 0.137251
In [366]: df1.sample(n=5)
Out[366]:
a b c d
3 0.578582 0.541716 0.451810 0.173890
4 0.210301 0.600485 0.184326 0.035092
1 0.630460 0.203675 0.305280 0.965628
0 0.774496 0.852985 0.257568 0.223773
5 0.583564 0.164262 0.958537 0.943357
In [367]: df2.sample(n=5)
Out[367]:
a b c d
2 0.171046 0.293104 0.317514 0.831424
5 0.137273 0.085598 0.874845 0.917356
6 0.356898 0.781540 0.091851 0.173430
3 0.370028 0.566356 0.895919 0.440559
0 0.340624 0.755825 0.569149 0.543630
Upvotes: 1