Implement SMOTE-ENN
Implement SMOTE-ENN for imbalance in this Applied AI engineering tutorial — hands-on steps, troubleshooting, and what to study next.
Focus: implement smote-enn for imbalance
You’ve trained a classifier on a real-world dataset, and the accuracy looks amazing—95%! But when you inspect the confusion matrix, the model has completely ignored the minority class: it predicts the majority class for nearly every sample. This is the classic class imbalance trap, and it silently destroys model performance on the cases you actually care about: fraud transactions, rare diseases, or churned customers. Accuracy becomes a lie, and your model is essentially useless for the problem you set out to solve. In this lesson, you’ll learn how to implement SMOTE-ENN, a powerful hybrid resampling technique that not only oversamples the minority class but also cleans the noise from the dataset, giving your model a fair chance at learning both classes.
The problem this lesson solves
Class imbalance occurs when one class (the majority) vastly outnumbers another (the minority). In binary classification, a 99:1 split is common in fraud detection or medical diagnosis. Models trained on such data quickly learn to predict the majority class because it minimizes the loss function, achieving high accuracy without learning any real patterns. But accuracy as a metric fails you here—a model that always predicts 'no fraud' gets 99% accuracy and misses every actual fraud.
The core issue is that standard machine learning algorithms assume a roughly balanced class distribution. When the minority class is underrepresented, the decision boundary shifts toward the majority, and the model becomes biased. Resampling techniques, like SMOTE-ENN, address this by adjusting the training data distribution before feeding it to the model. Without resampling, your model may score well on accuracy but fail on precision, recall, and F1-score for the minority class—the very metrics that matter in real applications.
Why not just duplicate minority samples? Simply copying minority instances (random oversampling) overfits the model to exact duplicates and fails to generalize. SMOTE-ENN takes a smarter approach.
Core concept / mental model
Think of your dataset as a living community. The majority class is a large, dense city, while the minority class is a small, scattered village. A model trained on this data only learns the city’s geography. SMOTE-ENN (Synthetic Minority Over-sampling Technique + Edited Nearest Neighbors) works in two stages:
- SMOTE (oversampling): It manufactures new, synthetic minority instances by interpolating between existing minority samples. This is like building new houses between the village houses—expanding the minority community without making exact copies.
- ENN (undersampling): It then removes noisy samples from both classes. If a sample’s nearest neighbors disagree with its class, it’s likely noise or borderline—so we delete it. This cleans the dataset, removing ambiguous points that would confuse the model.
The result is a balanced, cleaner dataset that helps the model draw a more accurate decision boundary. It’s a hybrid approach—combining oversampling and undersampling—to mitigate the risks of each method alone.
In Python, the imbalanced-learn library (often abbreviated as imblearn) provides a one-line implementation: SMOTEENN. But to understand it fully, we’ll build it step-by-step and then use the built-in class.
How it works step by step
SMOTE-ENN combines two algorithms sequentially. Let’s break down each stage.
Step 1: SMOTE — Synthesize minority samples
For each minority sample, SMOTE finds its k nearest neighbors (usually 5) within the minority class. It then selects one neighbor at random and creates a synthetic sample by interpolating the feature values between the two points. Mathematically, for a sample x_i and neighbor x_j, the new sample is:
x_new = x_i + λ * (x_j - x_i)
where λ is a random number between 0 and 1. This creates a point along the line segment between the two original points. By repeating this process, you can generate as many synthetic samples as needed to balance the class distribution.
Step 2: ENN — Clean noisy samples
After SMOTE has oversampled the minority class, ENN applies the edited nearest neighbor rule to the entire dataset (both classes). For each sample, it identifies its k nearest neighbors (commonly 3). If the sample’s class does not agree with the majority class of its neighbors, the sample is removed. This eliminates noise and borderline samples that could confuse the classifier. ENN is more aggressive than Tomek Links—it removes any sample whose neighbors contradict its label, not just pairs.
Step 3: Resulting dataset
The final dataset is a balanced and cleaned version of the original. You can then train any classifier on this transformed data and expect better performance on the minority class.
Hands-on walkthrough
Let’s implement SMOTE-ENN in Python. First, ensure you have the necessary libraries installed:
pip install imbalanced-learn scikit-learn pandas
Now, let’s create a synthetic imbalanced dataset and apply SMOTE-ENN.
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
from imblearn.combine import SMOTEENN
# Generate an imbalanced dataset (2000 samples, 95% majority, 5% minority)
X, y = make_classification(
n_samples=2000,
n_features=10,
n_informative=6,
n_redundant=2,
n_clusters_per_class=1,
weights=[0.95, 0.05],
random_state=42
)
print("Original class distribution:")
print(pd.Series(y).value_counts(normalize=True))
Expected output (approximate, since it’s random):
Original class distribution:
0 0.95
1 0.05
Name: proportion, dtype: float64
Now apply SMOTE-ENN:
# Apply SMOTE-ENN
smote_enn = SMOTEENN(random_state=42)
X_resampled, y_resampled = smote_enn.fit_resample(X, y)
# Check the new distribution
print("\nResampled class distribution:")
print(pd.Series(y_resampled).value_counts(normalize=True))
Expected output might show a more balanced, but not exactly 50/50, because ENN removes noisy samples. Something like:
Resampled class distribution:
0 0.58
1 0.42
Name: proportion, dtype: float64
Now train a model with and without resampling to see the difference:
# Split the original data (before resampling) for a fair test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Model without resampling
clf_raw = RandomForestClassifier(random_state=42)
clf_raw.fit(X_train, y_train)
y_pred_raw = clf_raw.predict(X_test)
print("\nClassification report WITHOUT resampling:")
print(classification_report(y_test, y_pred_raw))
# Resample the training data only
X_train_res, y_train_res = smote_enn.fit_resample(X_train, y_train)
# Model with resampling
clf_res = RandomForestClassifier(random_state=42)
clf_res.fit(X_train_res, y_train_res)
y_pred_res = clf_res.predict(X_test)
print("\nClassification report WITH SMOTE-ENN:")
print(classification_report(y_test, y_pred_res))
The difference in recall and F1-score for class 1 will be striking. Typically, the recall for the minority class jumps from under 50% to over 80%, at the cost of a slight drop in precision.
Pro tip: Always resample the training data after the train/test split to avoid data leakage. Never resample the test set.
Compare options / when to choose what
SMOTE-ENN is not the only resampling method. Here’s a comparison with other common techniques:
| Method | Type | Pros | Cons | Best for |
|---|---|---|---|---|
| Random Oversampling | Oversampling | Simple, no parameters | Overfitting on duplicates | Quick baseline |
| Random Undersampling | Undersampling | Simple, reduces size | Loses information | Large datasets |
| SMOTE | Oversampling | Creates diverse synthetic samples | Can add noise | Moderate imbalance |
| SMOTE-ENN | Hybrid | Balances + cleans noise | Heavier computation | Noisy, imbalanced data |
| SMOTE-Tomek | Hybrid | Removes borderline samples | Less aggressive than ENN | Cleaner datasets |
Choose SMOTE-ENN when your dataset is both imbalanced and noisy. If you have a large dataset, the excessive computation might be a concern; in that case, try SMOTE-Tomek or plain SMOTE. For severe imbalance, you might combine SMOTE-ENN with class weights in your model for even better results.
Troubleshooting & edge cases
1. SMOTE-ENN fails on categorical features. SMOTE uses Euclidean distance, which does not handle categorical data well. Solution: Encode categorical variables, or use libraries like imbalanced-learn’s SMOTENC for categorical features, but note that SMOTE-ENN does not have a native categorical variant. Preprocess with one-hot encoding or target encoding.
2. Memory or runtime issues. SMOTE-ENN is computationally expensive. For huge datasets (millions of rows), consider using RandomUnderSampler or SMOTE with a subsample. You can also set n_jobs=-1 (if supported) to parallelize.
3. The resampled dataset is not exactly 50/50. That’s expected—ENN removes around 30-50% of samples depending on noise level. If you need exact balance, use pure SMOTE or adjust ENN’s kind_sel parameter.
4. Validation on imbalanced test set. Even after resampling, evaluate on the original test set. Resampling the test set would give misleading results.
5. Class overlapping after resampling. If the classes overlap heavily, SMOTE-ENN may remove too many minority samples, reducing the dataset size drastically. Check the final sample count; if too small, try SMOTE-Tomek instead.
What you learned & what's next
You’ve learned why class imbalance is a common problem that breaks standard classifiers, and how SMOTE-ENN solves it by combining oversampling with noise cleanup. You implemented it with imblearn in a few lines of code, saw the dramatic improvement in minority-class recall, and compared it with other resampling methods. Every learning objective is covered: you can explain the core idea, and you’ve completed a hands-on exercise.
Now that you can implement SMOTE-ENN, you’re ready to explore the next step in your Applied AI engineering path: handling imbalanced data with advanced ensemble methods, such as Balanced Random Forest or EasyEnsemble. These integrate resampling directly into the model training, which can be even more powerful. Keep the code you wrote here—you’ll reuse the resampling pipeline in upcoming lessons.
Practice recap
As a mini-exercise, take a real or synthetic imbalanced dataset of your choice, apply SMOTE-ENN, and compare the classification report with a model trained without resampling. Experiment with the n_neighbors parameter in SMOTE and kind_sel in ENN to see how they affect performance. Then, try SMOTE-Tomek and compare the two hybrids to decide which works better for your data.
Common mistakes
- Applying SMOTE-ENN to the entire dataset before splitting into train and test, causing data leakage and overly optimistic results.
- Using SMOTE-ENN on datasets with categorical features without encoding, leading to distorted synthetic samples.
- Ignoring the effect of ENN’s removal—your resampled dataset may become much smaller than expected; check the final size.
- Evaluating the model on the resampled test set, which inflates performance metrics and fails to reflect real-world conditions.
- Assuming SMOTE-ENN always gives balance; it often results in a near-balanced but not exactly 50/50 dataset.
Variations
- SMOTE-Tomek: A lighter hybrid that removes only borderline samples via Tomek links, preserving more data for noisy but moderate datasets.
- Plain SMOTE: Oversampling without the ENN cleaning step; useful when noise is not a major concern and you need exact balance.
- ADASYN (Adaptive Synthetic Sampling): Focuses on generating samples near the decision boundary, which can complement SMOTE-ENN in different scenarios.
Real-world use cases
- Fraud detection in credit card transactions where fraud cases are <1% of all transactions; resampling boosts recall for catching anomalies.
- Medical diagnosis of rare diseases where patient samples are scarce; SMOTE-ENN helps the model learn patterns from limited positive cases.
- Customer churn prediction in subscription services where churn rates are around 5-10%; resampling improves precision and recall for identifying at-risk customers.
Key takeaways
- Class imbalance severely biases models toward the majority class, making accuracy a misleading metric.
- SMOTE-ENN combines oversampling (SMOTE) with noise removal (ENN) to both balance and clean the dataset.
- Always split data before resampling to prevent data leakage.
- Evaluate performance on the original, imbalanced test set with metrics like recall and F1-score for the minority class.
- SMOTE-ENN is ideal for noisy, imbalanced data; for exact balance or huge datasets, consider alternatives like SMOTE or SMOTE-Tomek.
- Incorporate resampling into a pipeline with cross-validation for robust evaluation.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.