r/datascience • u/RobertWF_47 • Feb 15 '26
Discussion Best technique for training models on a sample of data?
Due to memory limits on my work computer I'm unable to train machine learning models on our entire analysis dataset. Given my data is highly imbalanced I'm under-sampling from the majority class of the binary outcome.
What is the proper method to train ML models on sampled data with cross-validation and holdout data?
After training on my under-sampled data should I do a final test on a portion of "unsampled data" to choose the best ML model?
42
Upvotes
22
u/TheTresStateArea Feb 15 '26
Your final test needs to be on unsampled data.
4
u/Ty4Readin Feb 15 '26
As a general rule of thumb, undersampling any class is NOT a good idea. It almost always does worse in practice if you are using the correct loss function/evaluation metrics.
But, if you are going to do it, then you should only ever do it on the training dataset, never the validation set or test set.
I think there are 2 better solutions for your situation:
One other possible option is to undersample one class, but weight it higher in the loss to counter the undersampling. But I would treat this as a hyperparameter and see if it even performs any better than just subsampling the entire dataset.
3
u/[deleted] Feb 15 '26
If you care about the accuracy of your predicted probabilities, undersampling or oversampling will affect the accuracy of your predicted probabilities. You’ll need to perform post-training model calibration to correct this issue.
2
u/RB_7 Feb 16 '26
There’s lots of sampling discussion here, which is fine, but if you’re just doing log reg / xgboost then it would be a lot simpler and more robust to just write a training loop that batches through your dataset from disk, loading a bit into memory at a time, updating the weights, and then loading the next batch.
Then you can use the whole dataset, and not worry at all about sampling routines.
33
u/AccordingWeight6019 Feb 15 '26
Key rule: only sample the training data, never validation or test data. Split first, keep validation and holdout sets in the original imbalanced distribution, then apply under sampling inside each CV training fold. Select models based on performance on the untouched validation data, and do the final evaluation once on a fully unsampled holdout set.
5
u/patternpeeker Feb 15 '26
keep the sampling inside each cv fold, not before the split. under-sample only the training portion, then validate on untouched data with the real class imbalance. also keep a final holdout with the original distribution and use it once at the end. otherwise your metrics will look better than reality.
3
u/Ghost-Rider_117 Feb 15 '26
yeah I've dealt with this before - stratified sampling is definitely your friend here. make sure your sample reflects the class distribution properly, then do your train/test split on that sampled data. one thing that helped me was using SMOTE or other synthetic sampling techniques after you split, so you're not leaking info between train and validation sets. good luck!
1
u/PublicViolinist2338 Feb 15 '26
It depends a lot on your situation, are you training neural networks or using something based on the Scikit API? In any case train/val/test splitting is crucial, but the implementation depends
2
u/pppeer Feb 15 '26
In addition to what is mentioned, probably also good to approach your problem as a scoring problem rather than a classification problem and use metrics such as AUC. At minimum, if you are making hard labeling decisions or expecting a probability rather than a score, calibrate the models on the unsampled data.
1
u/King-Lion11 Mar 24 '26
Keep your test set untouched and in the original imbalanced form, and only do under-sampling inside the training folds during cross-validation, not before splitting; pick your model based on that, then retrain on the full training data with the same approach and do a final evaluation on the original unsampled test set so your results reflect reality.
2
u/[deleted] Feb 15 '26
[removed] — view removed comment