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

Python
Data Preprocessing

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
 3|  
 4|  # Load dataset
 5|  data = pd.read_csv('titanic.csv')
 6|  
 7|  # Separate features and target variable
 8|  X = data.drop('Survived', axis=1)
 9|  y = data['Survived']
10|  
11|  # Perform SMOTE to oversample the minority class
12|  smote = SMOTE()
13|  X_resampled, y_resampled = smote.fit_resample(X, y)
1 Upvote
Did you find this snippet useful?

Sign up to bookmark this in your snippet library

Normalize Windowed Time Series
Python
Data Preprocessing

Scaler | Normalize | Scale | Min-max

4
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3