Duplicating training examples to handle class imbalance in a pandas data frame
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Duplicating minority-class examples is one of the simplest ways to handle class imbalance before training a model. It is not always the best method, but it is a strong baseline because it is easy to implement, easy to explain, and quick to compare against alternatives such as class weights or synthetic oversampling.
Check the Imbalance First
Before duplicating anything, inspect the class distribution so you know what problem you are solving.
If one class is rare, a classifier may learn to predict the majority class most of the time and still achieve deceptively high accuracy. That is why imbalance-aware metrics matter more than raw accuracy in these problems.
Oversample Only the Training Split
The most important rule is to split the data before duplicating rows. If you oversample first and then split, copies of the same minority example can leak into both train and test sets, which makes evaluation overly optimistic.
That split preserves the original label distribution in the test set so you can evaluate the model under realistic conditions.
Duplicate the Minority Class with Replacement
Once you have the training split, you can duplicate minority rows until the classes are balanced.
This is simple random oversampling. The minority class is represented more often during training, which can improve recall and make the learner pay more attention to that class.
Train and Evaluate with the Right Metrics
After oversampling, train on the balanced training frame and evaluate on the untouched test set.
Pay attention to precision, recall, and F1 score for the minority class. That gives a much better picture than overall accuracy.
Know the Tradeoffs of Duplication
Random duplication is easy, but it can also make the model memorize rare examples instead of learning a broader pattern, especially when the minority class is extremely small.
That does not mean the method is useless. It just means you should treat it as a baseline and compare it against other options:
- '
class_weight="balanced"in supported models' - synthetic methods such as SMOTE
- collecting more minority-class data if possible
If duplication improves recall significantly without unacceptable precision loss, it may already be good enough for the problem at hand.
Compare Against Class Weights
Some models support class weighting directly, which can avoid literal row duplication.
For linear models in particular, class weighting is often a strong competitor to oversampling. It is worth measuring both instead of assuming one is always better.
Common Pitfalls
The biggest mistake is oversampling before the train-test split. That leaks duplicated samples into evaluation and makes the results look better than they really are.
Another mistake is measuring success only with accuracy. In imbalanced problems, accuracy often tells you almost nothing about minority-class behavior.
Developers also sometimes oversample a tiny minority class until it dominates the training set. That can create heavy overfitting and unstable calibration.
Finally, remember to shuffle the balanced training frame after concatenation. If all majority rows come first and all duplicated minority rows come last, some training pipelines can behave oddly.
Summary
- Duplicate minority examples only after splitting the data.
- Use
resample(..., replace=True)for a simple oversampling baseline. - Train on the balanced training set and evaluate on the untouched test set.
- Focus on recall, precision, and F1 instead of accuracy alone.
- Compare duplication with class weights and synthetic oversampling methods.
Related reading
- Dynamic size for tf.zeros for use with placeholders with None dimensions
- Eager execution in Tensorflow 2
- Early stopping with Keras and sklearn GridSearchCV cross-validation
- Early stopping with multiple conditions
- DynamicFrame vs DataFrame
- Effective queries in machine learning
- Early stopping with tf.estimator, how?
- EarlyStopping is ignoring my custom metrics defined. Keras model
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.