Random Oversampling
Random oversampling gives selected classes more appearances during training by drawing existing rows with replacement. Replacement means a row remains eligible after it has been drawn, so the same row can be copied repeatedly.
No new measurement is collected. The useful question is whether giving existing examples more training influence improves the evaluation objective, compared with the original data and with class weighting.
Follow the row identities
Start with six negative examples A–F and two positive examples G and H. To reach six positives, retain the eight original rows and make four additional draws from {G, H}. One possible draw sequence is H, G, H, H.
The resulting training set has six negatives and six positives. G appears twice in total; H appears four times. Equal class counts do not imply that every member of a class is repeated equally.
Original rows: A–F are negative; G and H are positive.
Added copies (dashed border):
A target count is the final count
With sampling_strategy={1: 6}, the dictionary means “make class 1 contain six rows.” It does not mean “add six rows.” Since two positives already exist, the sampler adds four.
For binary classification, a float specifies the desired minority-to-majority count ratio after resampling. Here sampling_strategy=0.5 means three positives against six negatives, so it adds one positive. The ratio 0.5 means one positive per two negatives; the positive fraction of all rows is then 1/3, not 1/2.
A dictionary avoids ambiguity and also supports multiple classes. With the default "auto", RandomOverSampler brings the other classes up to the largest class’s count. Balancing every class is a candidate to validate, not a required preprocessing step.
Repeating a row acts like an integer loss weight
In sequence B, H is evaluated four times whenever we compute the loss over the complete resampled set. If its prediction is fixed, those four identical rows contribute four identical loss terms. G contributes two terms, and each negative contributes one.
Write the loss using the original row identities
Let $c_i$ be the number of times original row $i$ appears and $\ell_i$ its per-row loss. There are $n$ original rows and $N'=\sum_i c_i$ resampled rows. Their mean loss can be written as
For sequence B, the weights of G and H are 2 and 4. Across repeated uniform draws, each positive has expected multiplicity 3, but one realized sample need not give both that multiplicity. This is why random oversampling and a fixed equal weight for every positive are related without being identical datasets.
Full integer repetition matches an integer-weighted sum of losses. Actual training can still differ through regularization conventions, minibatch composition and optimization. An oversampled epoch contains more rows and, with a fixed batch size, more optimizer steps. Keep that training-budget change visible when comparing methods.
What the extra appearances cannot add
Repeated G rows do not supply new examples of a positive subtype absent from G and H. They also repeat any measurement or labeling mistake in those rows. A flexible learner can devote more effort to memorizing the repeated examples without improving on new positives.
The sampler’s counts therefore measure training appearances, not independent evidence. Evaluate on untouched observations, inspect minority subtypes and compare against a no-resampling baseline. Where the estimator accepts them, class weights can change the objective without storing extra rows.
By default, shrinkage=None produces exact copies. A positive shrinkage setting requests a smoothed bootstrap that perturbs features; that is a different assumption about valid nearby observations. The examples here use exact copying.
Split before copying, and sample only while fitting
If copies are made before a train/validation split, versions of the same observation can land on both sides. Validation then partly tests recognition of already-seen examples. Split original identities first, respecting any group or time boundary.
References and further reading
- Imbalanced-learn: RandomOverSampler — replacement sampling, final-count targets, source indices and smoothed-bootstrap settings.
- Imbalanced-learn: Pipeline — resampling during fit and the prediction-time contract.
- Imbalanced-learn: resampling leakage — why copying before splitting compromises evaluation.