r/datascience • u/harsh5161 • Dec 06 '21
Discussion Wish I could get same performance training and testing datasets in imbalanced dataset
33
u/GreatStats4ItsCost Dec 06 '21
Have you tried the class_weight hyper parameter?
18
u/Suolla Dec 06 '21
Similarly, when using xgboost, try the scale_pos_weight hyperparameter. Got really good results using that without having to balance the dataset
4
u/n7leadfarmer Dec 06 '21
Resampling your training data is also another thing to try, although I think I read that for xgboost scale_pos_weight is the 'best practice'.
3
u/Suolla Dec 06 '21
With both under and oversampling I tend to overfit the model very much on the training data. With having a minority positive class, resampling the training data tends to get me a large number of false positives in the test data. I might be doing something wrong there while resampling, but I don't face this issue as much when using the hyperparameter
1
u/n7leadfarmer Dec 06 '21
With xgboost, I think it's situationally specific, and as I've said the HP is the best practice. But I do think multiple methods should be tested, especially if your training set sees a big change over time (more data) or your preprocessing pipeline undergoes any moderate to large changes.
I built and currently maintain an xgboost model that has outperformed scale_pos_weight in multiple production scenarios.
8
u/musclecard54 Dec 06 '21
SMOTE also
12
Dec 06 '21
Just remember not to use do it before the split.
And not to use it if its a time series problem.
20
u/science-the-data Dec 06 '21
have you tried stratifying your train-test split?
12
Dec 06 '21
This is one of the easiest ways to take a stab at this issue. Sklearn train_test_split readily has a stratify parameter, if you have a well defined target
3
u/kunaguerooo123 Dec 06 '21
Stratify k fold?
3
Dec 06 '21
You can pass in the target array straight in the stratify parameter of sklearn train test split. That should do a stratified split of the target.
Otherwise, you have options: https://scikit-learn.org/stable/modules/cross_validation.html#stratification
1
u/science-the-data Dec 06 '21
I always stratify my k-folds when working with imbalanced data. Same principle as with train-treat split but you do it for all the folds.
1
u/kunaguerooo123 Dec 06 '21
Cool! I’m learning all these from the hands on machine learning book.. I get it intuitively why it works - but do you also read up research papers on its derivation? Or should I ignore that for now and focus on learning the tools (docker etc) and then go deep? There’s a lot of models out there and I thought it won’t make sense to randomly waste time going deep on one
13
u/automated_care Dec 06 '21
Stratifying and data augmentation. SMOTE, oversampling, undersampling.
If it's NLP you can do back translation, random deletion, and synonym replacement
9
u/ants_rock Dec 06 '21
Have you tried just having bad performance on the training set too? Then you'd achieve your stated goal.
4
u/111llI0__-__0Ill111 Dec 07 '21
The best most preferred way to deal with imbalanced classes is literally do nothing to the data, especially if it represents the natural distribution of the data generating process. You can alter the threshold for classification but if you do anything to the data itself its going to ruin the calibration of the predicted probabilties
4
2
1
171
u/GeorgeS6969 Dec 06 '21
Pro tip: pick your test set out of your training set then blame the lack of performance in production on COVID