Sitemap

โ€œ๐๐š๐ฅ๐š๐ง๐œ๐ข๐ง๐  ๐ˆ๐ฆ๐›๐š๐ฅ๐š๐ง๐œ๐ž๐ ๐ƒ๐š๐ญ๐š๐ฌ๐ž๐ญ๐ฌ: ๐€ ๐ƒ๐ž๐ž๐ฉ ๐ƒ๐ข๐ฏ๐ž ๐ข๐ง๐ญ๐จ ๐Œ๐š๐œ๐ก๐ข๐ง๐ž ๐‹๐ž๐š๐ซ๐ง๐ข๐ง๐  ๐’๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐  ๐“๐ž๐œ๐ก๐ง๐ข๐ช๐ฎ๐ž๐ฌโ€

4 min readMar 11, 2025

--

๐’๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐ :

Sampling plays a crucial role in machine learning, especially when dealing with imbalanced datasets. The choice of a sampling technique can significantly impact model performance by ensuring balanced data distribution and preventing bias. In this blog, weโ€™ll explore different sampling methods, including oversampling, undersampling and hybrid techniques like SMOTE, Borderline-SMOTE, and ADASYN.

Press enter or click to view image in full size
Image Describing Sampling

๐Ÿ. ๐–๐ก๐ฒ ๐ข๐ฌ ๐’๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐  ๐ˆ๐ฆ๐ฉ๐จ๐ซ๐ญ๐š๐ง๐ญ?

Machine learning models learn from data, and if the data is imbalanced, models may become biased toward the majority class, leading to poor generalization. Sampling techniques help by either balancing the dataset or selecting a representative subset, improving model robustness and accuracy.

๐Ÿ. ๐“๐ฒ๐ฉ๐ž๐ฌ ๐จ๐Ÿ ๐’๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐  ๐“๐ž๐œ๐ก๐ง๐ข๐ช๐ฎ๐ž๐ฌ:

Sampling methods can be broadly classified into:

Press enter or click to view image in full size
Types of Sampling
  1. Random Sampling
  2. Oversampling (Increasing the Minority Class)
  3. Undersampling (Reducing the Majority Class)
  4. Hybrid Techniques (Combining Over and Under Sampling)

๐Ÿ.๐Ÿ ๐‘๐š๐ง๐๐จ๐ฆ ๐’๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐ :

๐Ÿ“Œ ๐–๐ก๐š๐ญ ๐ˆ๐ญ ๐ˆ๐ฌ?

  • Random Oversampling: Duplicates existing minority class instances to balance the dataset.
  • Random Undersampling : Removes majority class samples to match the minority class size.
  • Risk: Can lead to overfitting (oversampling) or loss of valuable data (undersampling).

โœ… ๐–๐ก๐ž๐ง ๐ญ๐จ ๐”๐ฌ๐ž:

  • Use random oversampling when you have a small dataset and want to quickly balance classes.
  • Use random undersampling when you have a large dataset and removing data wonโ€™t impact performance significantly.

โŒ ๐–๐ก๐ž๐ง ๐๐จ๐ญ ๐ญ๐จ ๐”๐ฌ๐ž:

  • Avoid random oversampling when overfitting is a concern.
  • Avoid random undersampling when you risk losing important patterns in the data.

๐Ÿ.๐Ÿ ๐’๐ฒ๐ง๐ญ๐ก๐ž๐ญ๐ข๐œ ๐Œ๐ข๐ง๐จ๐ซ๐ข๐ญ๐ฒ ๐Ž๐ฏ๐ž๐ซ-๐ฌ๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐  ๐“๐ž๐œ๐ก๐ง๐ข๐ช๐ฎ๐ž (๐’๐Œ๐Ž๐“๐„):

๐Ÿ“Œ ๐–๐ก๐š๐ญ ๐ˆ๐ญ ๐ˆ๐ฌ?

SMOTE is a widely used technique that generates synthetic minority class samples instead of duplicating them. It works by selecting a sample from the minority class, finding its k-nearest neighbors, and interpolating new points along the line connecting them.

๐ˆ๐ฆ๐ฉ๐ฅ๐ž๐ฆ๐ž๐ง๐ญ๐š๐ญ๐ข๐จ๐ง ๐ข๐ง ๐๐ฒ๐ญ๐ก๐จ๐ง ๐Ÿ:

from imblearn.over_sampling import SMOTE
from collections import Counter

# Applying SMOTE
smote = SMOTE(sampling_strategy='auto', random_state=42)
X_resampled, y_resampled = smote.fit_resample(X, y)
print("After SMOTE:", Counter(y_resampled))

โœ… ๐–๐ก๐ž๐ง ๐ญ๐จ ๐”๐ฌ๐ž:

  • Use SMOTE when you need synthetic data without simply duplicating existing samples.
  • Works well when your dataset is moderately imbalanced.

โŒ ๐–๐ก๐ž๐ง ๐๐จ๐ญ ๐ญ๐จ ๐”๐ฌ๐ž:

  • Avoid SMOTE when dealing with highly overlapping class distributions, as it may introduce noise.
  • Not ideal when the dataset is too small, as synthetic samples might not generalize well.

๐Ÿ.๐Ÿ‘ ๐๐จ๐ซ๐๐ž๐ซ๐ฅ๐ข๐ง๐ž-๐’๐Œ๐Ž๐“๐„ (๐-๐’๐Œ๐Ž๐“๐„):

๐Ÿ“Œ ๐–๐ก๐š๐ญ ๐ˆ๐ญ ๐ˆ๐ฌ?

Unlike SMOTE, Borderline-SMOTE focuses only on hard-to-classify samples that lie close to the decision boundary.

  • B-SMOTE1: Generates synthetic data only for borderline minority samples.
  • B-SMOTE2: Adds both synthetic minority samples and some majority samples

๐ˆ๐ฆ๐ฉ๐ฅ๐ž๐ฆ๐ž๐ง๐ญ๐š๐ญ๐ข๐จ๐ง ๐ข๐ง ๐๐ฒ๐ญ๐ก๐จ๐ง๐Ÿ:

from imblearn.over_sampling import BorderlineSMOTE
bsmote = BorderlineSMOTE(kind='borderline-1', random_state=42)
X_resampled, y_resampled = bsmote.fit_resample(X, y)

โœ… ๐–๐ก๐ž๐ง ๐ญ๐จ ๐”๐ฌ๐ž:

  • Ideal when minority class samples are close to decision boundaries and need reinforcement.
  • Works well for datasets where the separation between classes is unclear.

โŒ ๐–๐ก๐ž๐ง ๐๐จ๐ญ ๐ญ๐จ ๐”๐ฌ๐ž:

  • Avoid when minority class samples are well-separated from the majority class.
  • May not be suitable if your dataset already has strong decision boundaries.

๐Ÿ.๐Ÿ’ ๐€๐๐š๐ฉ๐ญ๐ข๐ฏ๐ž ๐’๐ฒ๐ง๐ญ๐ก๐ž๐ญ๐ข๐œ ๐’๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐  (๐€๐ƒ๐€๐’๐˜๐):

๐Ÿ“Œ ๐–๐ก๐š๐ญ ๐ˆ๐ญ ๐ˆ๐ฌ?

ADASYN is an advanced version of SMOTE that focuses on harder-to-learn instances in the minority class. It assigns more weight to these instances and generates synthetic samples accordingly.

๐ˆ๐ฆ๐ฉ๐ฅ๐ž๐ฆ๐ž๐ง๐ญ๐š๐ญ๐ข๐จ๐ง ๐ข๐ง ๐๐ฒ๐ญ๐ก๐จ๐ง ๐Ÿ:

from imblearn.over_sampling import ADASYN
adasyn = ADASYN(sampling_strategy='auto', random_state=42)
X_resampled, y_resampled = adasyn.fit_resample(X, y)

โœ… ๐–๐ก๐ž๐ง ๐ญ๐จ ๐”๐ฌ๐ž:

  • Best for datasets where some minority samples are harder to classify than others.
  • Helps in improving classifier performance by focusing on difficult instances.

โŒ ๐–๐ก๐ž๐ง ๐๐จ๐ญ ๐ญ๐จ ๐”๐ฌ๐ž:

  • Avoid ADASYN if your dataset is small, as it may generate too much noise.
  • May not be the best choice when class distributions are already well-defined.

๐Ÿ‘. ๐‚๐จ๐ฆ๐ฉ๐š๐ซ๐ข๐ฌ๐จ๐ง ๐จ๐Ÿ ๐’๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐  ๐“๐ž๐œ๐ก๐ง๐ข๐ช๐ฎ๐ž๐ฌ:

Press enter or click to view image in full size
Comparison Table for Sampling Techniques

๐Ÿ’. ๐‚๐ก๐จ๐จ๐ฌ๐ข๐ง๐  ๐ญ๐ก๐ž ๐‘๐ข๐ ๐ก๐ญ ๐’๐š๐ฆ๐ฉ๐ฅ๐ข๐ง๐  ๐“๐ž๐œ๐ก๐ง๐ข๐ช๐ฎ๐ž:

  • Use SMOTE if you need a general oversampling approach.
  • Use Borderline-SMOTE if your dataset has a complex decision boundary.
  • Use ADASYN if you want to focus on difficult samples.
  • Use Random Sampling if you need a quick, simple solution.

๐‚๐จ๐ง๐œ๐ฅ๐ฎ๐ฌ๐ข๐จ๐ง:

Handling imbalanced data is one of the most crucial steps in building a reliable machine learning model. The right sampling technique can make all the difference between a biased model and one that generalizes well to unseen data.

Get Ayush_Argondaโ€™s stories inย yourย inbox

Join Medium for free to get updates fromย thisย writer.

โœ… Key Takeaways:

  • SMOTE is useful for generating synthetic minority samples without duplication.
  • Borderline-SMOTE improves classification near decision boundaries.
  • ADASYN focuses on harder-to-learn minority instances.
  • Random Sampling is quick but has risks like overfitting (oversampling) or information loss (undersampling).

๐Ÿ“Œ Next Steps:

  • Try implementing these techniques in your own ML projects.
  • Experiment with different sampling methods and compare results.
  • Read more on ensemble learning โ€” another powerful way to handle class imbalance.

๐Ÿ’ฌ Have you used any of these techniques in your machine learning projects? What worked best for you? Share your thoughts in the comments! If you found this guide helpful, share it with your network and follow me for more machine learning insights. ๐Ÿš€

--

--

Ayush_Argonda
Ayush_Argonda

Written by Ayush_Argonda

2024 IT Graduate | Data Science Consultant @Aligned Automation | Passionate about ML, AI, Generative and Agentic AI & Data Analytics | Lifelong learner!