Balancing The Imbalanced Datasets (SMOTE)
Imbalanced datasets are common in real world. Handling these imbalances is a challenging task. For example, In a loan repayment prediction, Out of 100 cases there could be less than 20 defaulters. When this data is passed through any model to predict defaulters, accuracy will be very low as the data is biased towards non-defaulters.

To handle these kind of scenarios we can use resampling techniques. Following are few types of resampling techniques:

Over Sampling: The minority class values are increased to match with the count of majority class.

The minority class values are increased to match with the count of majority class. Under Sampling: The majority class values are decreased to match with the count of minority class.

The majority class values are decreased to match with the count of minority class. Combination of Over Sampling and Under Sampling: The majority class values are decreased and minority class values are increased simultaneously.

The increment or decrement mentioned above can further be done using different ways. Please go through this document to know more about different techniques.

In this article, we’ll try to understand the impact of Over sampling using KMeansSmote on this sample data — Loan data

Please go through this kaggle notebook where I performed basic EDA and model building.

The following diagram shows the distribution of loan status data (class in this case)

Classes are distributed in 3:1:1 ratio

When logistic regression and decision trees are applied to the given data, F1 score was 0.26 and 0.28 respectively. Let’s try to apply sampling methods to overcome this issue.

To perform Over Sampling techniques, the following library need to be installed.

pip install imbalanced-learn==0.6.2

From this library, we can import KMeansSmote as shown below:

from imblearn.over_sampling import KMeansSMOTE

We can apply the KMeansSmote as shown below:

#X is the entire dataset except the class(i.e. loan_status)

#y is the class column (loan_status)

X = df_model.drop([‘loan_status’],axis=1)

y = df_model[‘loan_status’] smote = KMeansSMOTE(sampling_strategy='auto') #X_sm and y_sm is the resampled data

X_sm, y_sm = smote.fit_sample(X, y)

When logistic regression and decision trees are applied to the resampled data, F1 score was increased to 0.72 and 0.74 respectively.

I hope this might have helped you with the basic understanding of the impact of oversampling technique. We can still improve the accuracy by hyperparameter tuning.

Please find the notebook here for reference— kaggle notebook

Feel free to comment below.

Thank you :)
['Shiva Charan Pabba']
['Imbalanced Data', 'Machine Learning', 'Smote', 'Classification', 'Resampling']