In machine learning, dealing with imbalanced datasets is a common challenge. Imbalance happens when one class (usually the target variable) has significantly more examples than the other. This can lead to biased models that favor the majority class. A popular solution to this issue is SMOTE (Synthetic Minority Over-sampling Technique), which helps balance the dataset by generating synthetic samples for the minority class. Let's dive into how SMOTE works and how to use it in Python with a hands-on example! π
What is SMOTE? π€
SMOTE is an oversampling technique that creates synthetic examples of the minority class by interpolating between existing examples. Instead of duplicating instances, it generates new points that lie between existing ones, making the dataset more balanced without overfitting to repeated data.
Why Use SMOTE? βοΈ
- Balanced Datasets: SMOTE helps machine learning models by giving them enough data from all classes, so the models wonβt favor one class over another.
- Improved Model Accuracy: With a balanced dataset, models have a better chance to learn meaningful patterns from the minority class, improving overall accuracy.
- Effective for Small Datasets: When your minority class has very few examples, SMOTE can help without needing more data collection.
Example: Handling Imbalanced Dataset Using SMOTE
We'll now go through a step-by-step Python example to demonstrate how to handle imbalanced datasets using SMOTE.
Step 1: Import Required Libraries and Create a Dataset π
We begin by creating an imbalanced dataset using make_classification
from sklearn
.
from sklearn.datasets import make_classification
import pandas as pd
import matplotlib.pyplot as plt
# Create an imbalanced dataset
X, y = make_classification(n_samples=1000, n_redundant=0, n_features=2, n_clusters_per_class=1,
weights=[0.90], random_state=12)
# Convert to DataFrame for easier visualization
df1 = pd.DataFrame(X, columns=['f1', 'f2'])
df2 = pd.DataFrame(y, columns=['target'])
df = pd.concat([df1, df2], axis=1)
# Check the distribution of the target
print(df['target'].value_counts())
# Visualize the dataset
plt.scatter(df['f1'], df['f2'], c=df['target'])
plt.title("Imbalanced Dataset")
plt.show()
Output:
The dataset has two features, and the target variable is heavily imbalanced with 90% of the data belonging to class 0
and only 10% to class 1
. Here's the class distribution:
0 900
1 100
Name: target, dtype: int64
And here's a scatter plot showing the imbalance:
Step 2: Apply SMOTE to Balance the Dataset βοΈ
Now that we know our dataset is imbalanced, let's use SMOTE to balance it.
from imblearn.over_sampling import SMOTE
# Apply SMOTE
oversample = SMOTE()
X_res, y_res = oversample.fit_resample(df[['f1', 'f2']], df['target'])
# Convert the resampled data into a DataFrame
df1_res = pd.DataFrame(X_res, columns=['f1', 'f2'])
df2_res = pd.DataFrame(y_res, columns=['target'])
df_res = pd.concat([df1_res, df2_res], axis=1)
# Check the new distribution of the target
print(df_res['target'].value_counts())
# Visualize the balanced dataset
plt.scatter(df_res['f1'], df_res['f2'], c=df_res['target'])
plt.title("Balanced Dataset with SMOTE")
plt.show()
Output:
After applying SMOTE, the class distribution becomes balanced:
0 900
1 900
Name: target, dtype: int64
And the new scatter plot shows the balanced dataset:
Conclusion π―
Handling imbalanced datasets is crucial for building fair and accurate machine learning models. SMOTE is an excellent technique for oversampling the minority class and ensuring the model doesn't get biased towards the majority class. With SMOTE, you can improve model performance and get more meaningful insights from your data. π
Feel free to try SMOTE on your own datasets and experiment with different oversampling techniques! π
Top comments (0)