Balancing the Scale: A Comprehensive Approach to Imbalanced Datasets in Python
In this tutorial, we will learn how to deal with imbalanced datasets in the context of machine learning.
Imbalanced datasets are those in which the number of samples belonging to one class is significantly larger than the number of samples belonging to the other class(es). This can be a problem when building machine learning models, as the model is more likely to predict the class with more samples, leading to poor performance on the minority class.
In this tutorial, we will learn how to deal with imbalanced datasets in the context of machine learning.
1. Identifying imbalanced datasets
The first step in dealing with imbalanced datasets is to identify whether our dataset is imbalanced or not. One way to do this is to check the class distribution of the target variable.
For example, let’s say we have a dataset with a binary target variable, y, which can be either 0 or 1. We can check the class distribution using the value_counts method of the Pandas DataFrame:
import pandas as pd
df = pd.read_csv('my_dataset.csv')print(df['y'].value_counts())If the ratio of samples belonging to class 0 to samples belonging to class 1 is significantly large (e.g., 9), then we can consider our dataset to be imbalanced.
2. Balancing the dataset
Once we have identified that our dataset is imbalanced, we can try to balance it in order to improve the performance of our machine learning model. Here are some ways to balance an imbalanced dataset:
2.1. Undersampling
Undersampling involves reducing the number of samples in the majority class to be equal to the number of samples in the minority class. This can be done by randomly selecting a subset of the majority class samples.
# Separate majority and minority classesdf_majority = df[df['y']==0]df_minority = df[df['y']==1]
# Downsample majority classdf_majority_downsampled = df_majority.sample(n=len(df_minority), random_state=123)
# Combine minority class with downsampled majority classdf_downsampled = pd.concat([df_majority_downsampled, df_minority])2.2. Oversampling
Oversampling involves generating new samples in the minority class, using techniques such as bootstrapping or SMOTE (Synthetic Minority Oversampling TEchnique).
For example, to use SMOTE for oversampling, we can use the SMOTE function from the imblearn library:
from imblearn.over_sampling import SMOTE
smote = SMOTE(random_state=123)X_sm, y_sm = smote.fit_sample(X, y)2.3. Combining undersampling and oversampling
Another option is to combine both undersampling and oversampling, using techniques such as SMOTE-ENN (SMOTE + Edited Nearest Neighbors) or SMOTE-Tomek.
from imblearn.combine import SMOTEENN
smote_enn = SMOTEENN(random_state=123)X_smote_enn, y_smote_enn = smote_enn.fit_sample(X, y)3. Evaluating the performance of the model
Once we have balanced our dataset, we can use it to train a machine learning model and evaluate its performance. It is important to compare the performance of the model on the balanced dataset with the performance of the model on the original imbalanced dataset, to see if balancing the dataset has improved the model’s performance.
For example, we can train a random forest classifier on the balanced dataset and evaluate its performance using metrics such as accuracy, precision, and recall:
from sklearn.ensemble import RandomForestClassifierfrom sklearn.metrics import accuracy_score, precision_score, recall_score
# Train the modelmodel = RandomForestClassifier(random_state=123)model.fit(X_smote_enn, y_smote_enn)
# Make predictions on the test sety_pred = model.predict(X_test)
# Evaluate the modelaccuracy = accuracy_score(y_test, y_pred)precision = precision_score(y_test, y_pred)recall = recall_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.3f}')print(f'Precision: {precision:.3f}')print(f'Recall: {recall:.3f}')4. Wrapping it up
In this tutorial, we learned how to deal with imbalanced datasets in the context of machine learning. We saw three different approaches to balancing the dataset: undersampling, oversampling, and combining undersampling and oversampling. By balancing the dataset, we can improve the performance of our machine learning model and get better results on the minority class.
Comments