How to do under-sampling with K-fold validation in machine learning
You can use RandomUnderSampler method to achieve it. Put random states and ratio value in the arguments and try to see if this works.
I'm dealing with a highly imbalanced dataset for my project rn, for the simplicity, I will give a simple example here: a dataset has a number of 20 '0's and 80 '1's so the total is 100.
Suppose I have already used X_train, X_test,y_train,y_test = train_test_split(X, y,stratify=y,random_state=42) to make a stratified split (X_train.shape is 80 and X_test.shape is 20), so my question is how to achieve under-sampling with K-fold validation in the train dataset at the same time.
My initial thought is use from imblearn.under_sampling import RandomUnderSampler to get 16 '0's and 16 '1's (total is 32) to make equal distributed dataset, and do the K-fold cross-validation on that 32 dataset and discard the rest of 48 in the X_train. Use the model to predict the X_test. So I was wondering if this is correct procedure to deal with.