Balancing Imbalanced Datasets with SMOTE: A Python Example Using imbalanced-learn


In this example we're using the SMOTE class from the imblearn library to oversample the minority class in our dataset. First we load our dataset and separate the features and target variable. Then we create a SMOTE object and use its fit_resample() method to perform the oversampling. This method takes the features and target variable as input and returns the resampled data.

 1|  from imblearn.over_sampling import SMOTE
 2|  import pandas as pd
 4|  # Load dataset
 5|  data = pd.read_csv('titanic.csv')
 7|  # Separate features and target variable
 8|  X = data.drop('Survived', axis=1)
 9|  y = data['Survived']
11|  # Perform SMOTE to oversample the minority class
12|  smote = SMOTE()
13|  X_resampled, y_resampled = smote.fit_resample(X, y)
Did you find this snippet useful?

Sign up for free to to add this to your code library