🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #210

Oversampling

Oversampling rebalances a dataset by increasing the number of minority-class examples — the simplest version just duplicates existing minority samples until the classes are closer to balanced.

Random Oversampling — The Basic Approach

Using the reference dataset (950 majority, 50 minority): random oversampling duplicates minority samples, with replacement, until the target balance is reached — e.g. duplicating the 50 minority examples up to 950, producing a balanced 950/950 dataset of 1900 total rows.

from imblearn.over_sampling import RandomOverSampler
from collections import Counter

ros = RandomOverSampler(random_state=42)
X_resampled, y_resampled = ros.fit_resample(X_train, y_train)

print(Counter(y_train))        # {0: 950, 1: 50}
print(Counter(y_resampled))     # {0: 950, 1: 950} -- balanced by duplication

The Real Risk — Overfitting to Duplicated Points

Because oversampling literally repeats the same minority examples many times, a flexible model (especially KNN, decision trees, or neural networks) can start memorizing those exact repeated points rather than learning a general minority-class pattern — a distinctive overfitting risk unique to this technique. This is exactly the gap SMOTE addresses, by generating new, synthetic points instead of exact duplicates.

Applying It Correctly — Only on Training Data

from sklearn.model_selection import train_test_split
from imblearn.over_sampling import RandomOverSampler

# Split FIRST, on the original imbalanced data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)

# THEN oversample -- only the training set
ros = RandomOverSampler(random_state=42)
X_train_resampled, y_train_resampled = ros.fit_resample(X_train, y_train)

model.fit(X_train_resampled, y_train_resampled)
predictions = model.predict(X_test)   # X_test stays untouched, naturally imbalanced

The test set must always reflect the real-world class distribution the model will actually face — oversampling it would give a distorted, unrealistic picture of production performance.

Practical Use Cases

  • Smaller datasets, where undersampling's data-discarding tradeoff (see Undersampling) would throw away too much of the majority class
  • A quick first technique to try before moving to more sophisticated approaches like SMOTE

Advantages

  • Simple to implement and understand
  • Doesn't discard any original data, unlike undersampling

Limitations

  • Risk of overfitting to exactly duplicated minority points
  • Increases dataset size and training time

Common Mistakes

  • Oversampling before splitting into train/test — this leaks duplicated copies of the same underlying example into both sets, inflating test performance artificially.
  • Always targeting perfect 50/50 balance by default — a more moderate ratio is sometimes a better fit, worth testing rather than assuming.

Interview Relevance

Q: "Why must oversampling happen after the train/test split, not before?" Oversampling before splitting can place duplicated copies of the same original minority example into both the training and test sets — the model then gets evaluated partly on examples nearly identical to ones it trained on, artificially inflating reported test performance.

Practice Question

You oversample the minority class to perfect 50/50 balance and see training accuracy jump to near 100%, but test recall barely improves. What might be happening?

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Oversampling – FAQs

Quick answers about learning Oversampling in Machine Learning.

This free note from CodingNow 2.0 explains Oversampling in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Oversampling, is 100% free with no signup required.
With focused practice, most students grasp Oversampling in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now