Skip to content
Mar 1

Imbalanced Classification with SMOTE and ADASYN

MT
Mindli Team

AI-Generated Content

Imbalanced Classification with SMOTE and ADASYN

When your machine learning dataset has vastly more examples of one class than another, standard algorithms often fail you. They become biased toward predicting the majority class, missing the rare but critical cases—like fraudulent transactions, disease diagnoses, or machine failures. This is the challenge of imbalanced classification. Simply duplicating the few minority samples you have leads to overfitting. Instead, modern solutions involve intelligently creating new, plausible examples for the minority class to balance the training landscape before the model ever sees it.

From Random Oversampling to Intelligent Synthesis

The most naive approach to class imbalance is random oversampling, which randomly duplicates existing minority class instances. While it balances class counts, it provides no new information to the model and can severely exacerbate overfitting, as the model may simply memorize the repeated samples.

The breakthrough came with the Synthetic Minority Oversampling Technique (SMOTE). Instead of duplication, SMOTE generates synthetic examples. For a given minority instance, SMOTE:

  1. Finds its k-nearest neighbors (typically k=5) from the minority class.
  2. Randomly selects one of these neighbors.
  3. Creates a new synthetic sample along the line segment connecting the two points.

Mathematically, for a selected seed instance and a randomly chosen neighbor , a new synthetic sample is generated as: where is a random number between 0 and 1. This interpolation-based oversampling effectively populates the convex space between minority instances, making the minority region more general and less specific.

Adaptive Synthetic Sampling (ADASYN) builds on SMOTE with a crucial adaptation: it focuses synthesis on areas where minority examples are hardest to learn. ADASYN's algorithm is density-adaptive. First, it calculates the ratio of majority to minority neighbors for each minority instance, which serves as a density measure. Instances in "sparser" areas (surrounded by more majority class samples) are considered harder to learn from and are assigned a higher weight for synthetic sample generation. Consequently, ADASYN generates more synthetic data for those borderline and harder-to-learn minority instances. This adaptiveness often leads to improved performance over basic SMOTE when the class boundary is highly imbalanced and complex, though it can also introduce noise if the initial minority distribution is highly fragmented.

Advanced Variants: Targeting the Border and Cleaning Data

As researchers applied SMOTE, they identified specific scenarios needing refinement, leading to powerful variants.

Borderline-SMOTE operates on a key insight: the most critical samples for defining a classifier's decision boundary are those near the border with the majority class. This method first identifies "dangerous" minority instances—those where more than half of their k-nearest neighbors are from the majority class. It then applies the SMOTE synthesis process only to these borderline instances. By focused generation on the frontier, it strengthens the definition of the class boundary without cluttering the interior of the minority class cluster with potentially unnecessary synthetic points.

While oversampling adds minority samples, sometimes the best approach is a combination of over and undersampling. This is where SMOTE-Tomek and SMOTE-ENN come in.

  • SMOTE-Tomek Link: First, SMOTE is applied to generate synthetic minority samples. Then, Tomek Links are identified. A Tomek Link is a pair of instances from opposite classes that are each other's nearest neighbors. These pairs often lie on the class boundary or represent noise. The Tomek Links method typically removes the majority class instance from each pair, effectively cleaning the space between classes. The combined approach balances the dataset and then refines the class boundary.
  • SMOTE-Edited Nearest Neighbors (SMOTE-ENN): This is generally more aggressive. After applying SMOTE, the ENN rule is used: any instance (from any class) whose class label differs from the class of at least two of its three nearest neighbors is removed. This cleans both majority and minority class regions of noisy and mislabeled examples, often leading to a more well-defined and generalizable decision region. SMOTE-ENN can sometimes result in a significantly smaller, but much cleaner, final training set.

The Critical Pipeline: Avoiding Data Leakage with Resampling

A paramount, often catastrophic mistake is applying SMOTE to your entire dataset before splitting it into training and testing sets. This causes data leakage, as information from the test set (via the nearest-neighbor calculations during synthesis) contaminates the training process. Your model's performance metrics will become wildly optimistic and entirely untrustworthy.

The proper application requires that resampling be performed only on the training fold within each step of your model validation pipeline. For cross-validation pipelines, this means integrating the resampler into the modeling workflow itself. Using scikit-learn, you would employ a Pipeline object. For example:

pipeline = make_pipeline(SMOTE(random_state=42), StandardScaler(), RandomForestClassifier())

This pipeline is then passed to cross_val_score or GridSearchCV. During each cross-validation fold, SMOTE is fit only on the training portion of that fold, transforms that training data, and the classifier is trained on the resampled result. The validation fold is left completely untouched and in its original, imbalanced state, providing a realistic estimate of model performance on unseen data. This methodology is essential for getting valid, reproducible results.

Resampling vs. Cost-Sensitive Learning: Choosing Your Strategy

Synthetic oversampling is not the only weapon against imbalance. Cost-sensitive learning is a fundamentally different, yet complementary, approach. Instead of manipulating the data, it manipulates the learning algorithm by assigning a higher penalty to misclassifying minority class instances. Many algorithms, like LogisticRegression (via the class_weight='balanced' parameter) or RandomForestClassifier (via class_weight), have built-in mechanisms to do this.

So, when should you use SMOTE versus a cost-sensitive method?

  • Use SMOTE/ADASYN when you want to work with a balanced dataset to use algorithms that have no native cost-sensitive option, or when you suspect the imbalance has led to a lack of representative data for the minority class. It's a data-centric solution.
  • Use Cost-Sensitive Learning when you have a firm understanding of the relative "cost" of different types of errors (e.g., the financial cost of missing fraud vs. flagging a legitimate transaction) and can encode it directly. It's also simpler, as it avoids the stochasticity of synthetic data generation.

The most rigorous approach is to compare resampling with cost-sensitive learning approaches empirically within your validation framework. You should test:

  1. Your base model on the imbalanced data.
  2. Your model with integrated SMOTE/ADASYN in a pipeline.
  3. Your model with built-in cost-sensitive weights.

Often, a combination of both—light SMOTE oversampling combined with slight class weighting—can yield the best, most robust performance.

Common Pitfalls

  1. Leaking Information from the Test Set: As emphasized, applying resampling before a train-test split invalidates your entire experiment. Always use a pipeline to contain the resampling within the cross-validation loop.
  1. Blindly Applying SMOTE to All Imbalance Problems: SMOTE generates data in the feature space. If your minority class is not a coherent cluster but is instead several disconnected subpopulations or is heavily intertwined with the majority class, SMOTE can generate nonsensical, noisy samples in the void between clusters, degrading performance. Always visualize your data in reduced dimensions (e.g., using PCA or t-SNE) to assess the feasibility of interpolation.
  1. Ignoring the Impact of Noise: Basic SMOTE can amplify noise by interpolating between a noisy outlier and a clean instance. Techniques like SMOTE-ENN or borderline-SMOTE are designed to mitigate this. If your data is noisy, start with these more robust variants instead of vanilla SMOTE.
  1. Forgetting to Tune Resampling Hyperparameters: SMOTE's k_neighbors parameter, ADASYN's density threshold, and the choice of undersampler in a hybrid method are all hyperparameters. They should be tuned alongside your model's hyperparameters within the inner loop of a nested cross-validation to find the optimal data-modelling combination.

Summary

  • SMOTE addresses class imbalance by generating synthetic minority samples via linear interpolation between existing instances, preventing the overfitting caused by simple duplication.
  • ADASYN extends SMOTE by adopting a density-adaptive synthesis strategy, generating more data for harder-to-learn minority instances near complex boundaries or sparse regions.
  • Advanced variants like Borderline-SMOTE focus generation on critical boundary instances, while hybrids like SMOTE-ENN and SMOTE-Tomek combine oversampling with intelligent undersampling to clean the resulting dataset.
  • The proper application in cross-validation pipelines is non-negotiable; resampling must be performed only on the training folds to prevent data leakage and obtain valid performance estimates.
  • Synthetic oversampling should be compared with cost-sensitive learning approaches, as both are valid strategies for handling imbalance, and the best choice is problem-dependent and must be validated empirically.

Write better notes with AI

Mindli helps you capture, organize, and master any subject with AI-powered summaries and flashcards.