โ๐๐๐ฅ๐๐ง๐๐ข๐ง๐ ๐๐ฆ๐๐๐ฅ๐๐ง๐๐๐ ๐๐๐ญ๐๐ฌ๐๐ญ๐ฌ: ๐ ๐๐๐๐ฉ ๐๐ข๐ฏ๐ ๐ข๐ง๐ญ๐จ ๐๐๐๐ก๐ข๐ง๐ ๐๐๐๐ซ๐ง๐ข๐ง๐ ๐๐๐ฆ๐ฉ๐ฅ๐ข๐ง๐ ๐๐๐๐ก๐ง๐ข๐ช๐ฎ๐๐ฌโ
๐๐๐ฆ๐ฉ๐ฅ๐ข๐ง๐ :
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.
๐. ๐๐ก๐ฒ ๐ข๐ฌ ๐๐๐ฆ๐ฉ๐ฅ๐ข๐ง๐ ๐๐ฆ๐ฉ๐จ๐ซ๐ญ๐๐ง๐ญ?
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:
- Random Sampling
- Oversampling (Increasing the Minority Class)
- Undersampling (Reducing the Majority Class)
- 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.
๐. ๐๐จ๐ฆ๐ฉ๐๐ซ๐ข๐ฌ๐จ๐ง ๐จ๐ ๐๐๐ฆ๐ฉ๐ฅ๐ข๐ง๐ ๐๐๐๐ก๐ง๐ข๐ช๐ฎ๐๐ฌ:
๐. ๐๐ก๐จ๐จ๐ฌ๐ข๐ง๐ ๐ญ๐ก๐ ๐๐ข๐ ๐ก๐ญ ๐๐๐ฆ๐ฉ๐ฅ๐ข๐ง๐ ๐๐๐๐ก๐ง๐ข๐ช๐ฎ๐:
- 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. ๐








