The Wayback Machine - https://web.archive.org/web/20240913165306/https://www.geeksforgeeks.org/ml-handling-imbalanced-data-with-smote-and-near-miss-algorithm-in-python/
Open In App

ML | Handling Imbalanced Data with SMOTE and Near Miss Algorithm in Python

Last Updated : 14 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In Machine Learning and Data Science we often come across a term called

Imbalanced Data Distribution

, generally happens when observations in one of the class are much higher or lower than the other classes. As Machine Learning algorithms tend to increase accuracy by reducing the error, they do not consider the class distribution. This problem is prevalent in examples such as

Fraud Detection

,

Anomaly Detection

,

Facial recognition

etc. Standard ML techniques such as Decision Tree and Logistic Regression have a bias towards the

majority

class, and they tend to ignore the minority class. They tend only to predict the majority class, hence, having major misclassification of the minority class in comparison with the majority class. In more technical words, if we have imbalanced data distribution in our dataset then our model becomes more prone to the case when minority class has negligible or very lesser

recall

.

Imbalanced Data Handling Techniques:

There are mainly 2 mainly algorithms that are widely used for handling imbalanced class distribution.

  1. SMOTE
  2. Near Miss Algorithm

SMOTE (Synthetic Minority Oversampling Technique) – Oversampling

SMOTE (synthetic minority oversampling technique) is one of the most commonly used oversampling methods to solve the imbalance problem. It aims to balance class distribution by randomly increasing minority class examples by replicating them. SMOTE synthesises new minority instances between existing minority instances. It generates the

virtual training records by linear interpolation

for the minority class. These synthetic training records are generated by randomly selecting one or more of the k-nearest neighbors for each example in the minority class. After the oversampling process, the data is reconstructed and several classification models can be applied for the processed data.

More Deep Insights of how SMOTE Algorithm work !

  • Step 1: Setting the minority class set A, for each [Tex]$x \in A$[/Tex], the k-nearest neighbors of x are obtained by calculating the Euclidean distance between x and every other sample in set A.
  • Step 2: The sampling rate N is set according to the imbalanced proportion. For each [Tex]$x \in A$[/Tex], N examples (i.e x1, x2, …xn) are randomly selected from its k-nearest neighbors, and they construct the set [Tex]$A_1$[/Tex] .
  • Step 3: For each example [Tex]$x_k \in A_1$[/Tex] (k=1, 2, 3…N), the following formula is used to generate a new example: [Tex]$x’ = x + rand(0, 1) * \mid x – x_k \mid$[/Tex] in which rand(0, 1) represents the random number between 0 and 1.

  • NearMiss Algorithm – Undersampling

    NearMiss is an under-sampling technique. It aims to balance class distribution by randomly eliminating majority class examples. When instances of two different classes are very close to each other, we remove the instances of the majority class to increase the spaces between the two classes. This helps in the classification process. To prevent problem of

    information loss

    in most under-sampling techniques,

    near-neighbor

    methods are widely used.

    The basic intuition about the working of near-neighbor methods is as follows:

  • Step 1: The method first finds the distances between all instances of the majority class and the instances of the minority class. Here, majority class is to be under-sampled.
  • Step 2: Then, n instances of the majority class that have the smallest distances to those in the minority class are selected.
  • Step 3: If there are k instances in the minority class, the nearest method will result in k*n instances of the majority class.
  • For finding n closest instances in the majority class, there are several variations of applying NearMiss Algorithm :

    1. NearMiss – Version 1 : It selects samples of the majority class for which average distances to the k closest instances of the minority class is smallest.
    2. NearMiss – Version 2 : It selects samples of the majority class for which average distances to the k farthest instances of the minority class is smallest.
    3. NearMiss – Version 3 : It works in 2 steps. Firstly, for each minority class instance, their M nearest-neighbors will be stored. Then finally, the majority class instances are selected for which the average distance to the N nearest-neighbors is the largest.

    This article helps in better understanding and hands-on practice on how to choose best between different imbalanced data handling techniques.

    Load libraries and data file

    The dataset consists of transactions made by credit cards. This dataset has

    492 fraud transactions out of 284, 807 transactions

    . That makes it highly unbalanced, the positive class (frauds) account for 0.172% of all transactions.

    Python

    # import necessary modules import pandas as pd import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import LogisticRegression from sklearn.preprocessing import StandardScaler from sklearn.metrics import confusion_matrix, classification_report # load the data set data = pd.read_csv('creditcard.csv') # print info about columns in the dataframe print(data.info())

    Output:

    RangeIndex: 284807 entries, 0 to 284806
    Data columns (total 31 columns):
    Time 284807 non-null float64
    V1 284807 non-null float64
    V2 284807 non-null float64
    V3 284807 non-null float64
    V4 284807 non-null float64
    V5 284807 non-null float64
    V6 284807 non-null float64
    V7 284807 non-null float64
    V8 284807 non-null float64
    V9 284807 non-null float64
    V10 284807 non-null float64
    V11 284807 non-null float64
    V12 284807 non-null float64
    V13 284807 non-null float64
    V14 284807 non-null float64
    V15 284807 non-null float64
    V16 284807 non-null float64
    V17 284807 non-null float64
    V18 284807 non-null float64
    V19 284807 non-null float64
    V20 284807 non-null float64
    V21 284807 non-null float64
    V22 284807 non-null float64
    V23 284807 non-null float64
    V24 284807 non-null float64
    V25 284807 non-null float64
    V26 284807 non-null float64
    V27 284807 non-null float64
    V28 284807 non-null float64
    Amount 284807 non-null float64
    Class 284807 non-null int64

    Python

    # normalise the amount column data['normAmount'] = StandardScaler().fit_transform(np.array(data['Amount']).reshape(-1, 1)) # drop Time and Amount columns as they are not relevant for prediction purpose data = data.drop(['Time', 'Amount'], axis = 1) # as you can see there are 492 fraud transactions. data['Class'].value_counts()

    Output:

    0 284315
    1 492

    Split the data into test and train sets

    Python

    from sklearn.model_selection import train_test_split # split into 70:30 ration X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0) # describes info about train and test set print("Number transactions X_train dataset: ", X_train.shape) print("Number transactions y_train dataset: ", y_train.shape) print("Number transactions X_test dataset: ", X_test.shape) print("Number transactions y_test dataset: ", y_test.shape)

    Output:

    Number transactions X_train dataset: (199364, 29)
    Number transactions y_train dataset: (199364, 1)
    Number transactions X_test dataset: (85443, 29)
    Number transactions y_test dataset: (85443, 1)

    Now train the model without handling the imbalanced class distribution

    Python

    # logistic regression object lr = LogisticRegression() # train the model on train set lr.fit(X_train, y_train.ravel()) predictions = lr.predict(X_test) # print classification report print(classification_report(y_test, predictions))

    Output:


    precision recall f1-score support

    0 1.00 1.00 1.00 85296
    1 0.88 0.62 0.73 147

    accuracy 1.00 85443
    macro avg 0.94 0.81 0.86 85443
    weighted avg 1.00 1.00 1.00 85443

    The accuracy comes out to be 100% but did you notice something strange ?

    The recall of the minority class in very less. It proves that the model is more biased towards majority class. So, it proves that this is not the best model. Now, we will apply different

    imbalanced data handling techniques

    and see their accuracy and recall results.

    Using SMOTE Algorithm

    Python

    print("Before OverSampling, counts of label '1': {}".format(sum(y_train == 1))) print("Before OverSampling, counts of label '0': {} \n".format(sum(y_train == 0))) # import SMOTE module from imblearn library # pip install imblearn (if you don't have imblearn in your system) from imblearn.over_sampling import SMOTE sm = SMOTE(random_state = 2) X_train_res, y_train_res = sm.fit_resample(X_train, y_train.ravel()) print('After OverSampling, the shape of train_X: {}'.format(X_train_res.shape)) print('After OverSampling, the shape of train_y: {} \n'.format(y_train_res.shape)) print("After OverSampling, counts of label '1': {}".format(sum(y_train_res == 1))) print("After OverSampling, counts of label '0': {}".format(sum(y_train_res == 0)))

    Output:

    Before OverSampling, counts of label '1': [345]
    Before OverSampling, counts of label '0': [199019]

    After OverSampling, the shape of train_X: (398038, 29)
    After OverSampling, the shape of train_y: (398038, )

    After OverSampling, counts of label '1': 199019
    After OverSampling, counts of label '0': 199019

    Look!

    that SMOTE Algorithm has oversampled the minority instances and made it equal to majority class. Both categories have equal amount of records. More specifically, the minority class has been increased to the total number of majority class. Now see the accuracy and recall results after applying SMOTE algorithm (Oversampling).

    Prediction and Recall

    Python

    lr1 = LogisticRegression() lr1.fit(X_train_res, y_train_res.ravel()) predictions = lr1.predict(X_test) # print classification report print(classification_report(y_test, predictions))

    Output:

    precision recall f1-score support

    0 1.00 0.98 0.99 85296
    1 0.06 0.92 0.11 147

    accuracy 0.98 85443
    macro avg 0.53 0.95 0.55 85443
    weighted avg 1.00 0.98 0.99 85443

    Wow

    , We have reduced the accuracy to 98% as compared to previous model but the recall value of minority class has also improved to 92 %. This is a good model compared to the previous one. Recall is great. Now, we will apply NearMiss technique to Under-sample the majority class and see its accuracy and recall results.

    NearMiss Algorithm:

    Python

    print("Before Undersampling, counts of label '1': {}".format(sum(y_train == 1))) print("Before Undersampling, counts of label '0': {} \n".format(sum(y_train == 0))) # apply near miss from imblearn.under_sampling import NearMiss nr = NearMiss() X_train_miss, y_train_miss = nr.fit_resample(X_train, y_train.ravel()) print('After Undersampling, the shape of train_X: {}'.format(X_train_miss.shape)) print('After Undersampling, the shape of train_y: {} \n'.format(y_train_miss.shape)) print("After Undersampling, counts of label '1': {}".format(sum(y_train_miss == 1))) print("After Undersampling, counts of label '0': {}".format(sum(y_train_miss == 0)))

    Output:

    Before Undersampling, counts of label '1': [345]
    Before Undersampling, counts of label '0': [199019]

    After Undersampling, the shape of train_X: (690, 29)
    After Undersampling, the shape of train_y: (690, )

    After Undersampling, counts of label '1': 345
    After Undersampling, counts of label '0': 345

    The

    NearMiss Algorithm

    has undersampled the majority instances and made it equal to majority class. Here, the majority class has been reduced to the total number of minority class, so that both classes will have equal number of records.

    Prediction and Recall

    Python

    # train the model on train set lr2 = LogisticRegression() lr2.fit(X_train_miss, y_train_miss.ravel()) predictions = lr2.predict(X_test) # print classification report print(classification_report(y_test, predictions))

    Output:

    precision recall f1-score support

    0 1.00 0.56 0.72 85296
    1 0.00 0.95 0.01 147

    accuracy 0.56 85443
    macro avg 0.50 0.75 0.36 85443
    weighted avg 1.00 0.56 0.72 85443

    This model is better than the first model because it classifies better and also the recall value of minority class is 95 %. But due to undersampling of majority class, its recall has decreased to 56 %. So in this case, SMOTE is giving me a great accuracy and recall, I’ll go ahead and use that model! 🙂



    Similar Reads

    How to Use SMOTE for Imbalanced Data in R
    In the field of machine learning, dealing with unbalanced datasets is a common challenge. When one class significantly outnumbers the other, models tend to be biased towards the majority class, resulting in poor predictive performance for the minority class. Synthetic Minority Over-sampling Technique (SMOTE) is an effective method to address this i
    5 min read
    SMOTE for Imbalanced Classification with Python
    Imbalanced datasets impact the performance of the machine learning models and the Synthetic Minority Over-sampling Technique (SMOTE) addresses the class imbalance problem by generating synthetic samples for the minority class. The article aims to explore the SMOTE, its working procedure, and various extensions to enhance its capability. The article
    14 min read
    Handling Imbalanced Data for Classification
    A key component of machine learning classification tasks is handling unbalanced data, which is characterized by a skewed class distribution with a considerable overrepresentation of one class over the others. The difficulty posed by this imbalance is that models may exhibit inferior performance due to bias towards the majority class. When faced wit
    12 min read
    Handling imbalanced classes in CatBoost: Techniques and Solutions
    Gradient boosting algorithms have become a cornerstone in machine learning, particularly in handling complex datasets with heterogeneous features and noisy data. One of the most prominent gradient boosting libraries is CatBoost, known for its ability to process categorical features effectively. However, like other boosting algorithms, CatBoost face
    8 min read
    Classification on Imbalanced data using Tensorflow
    In the modern days of machine learning, imbalanced datasets are like a curse that degrades the overall model performance in classification tasks. In this article, we will implement a Deep learning model using TensorFlow for classification on a highly imbalanced dataset. Classification on Imbalanced data using Tensorflow What is Imbalanced Data?Most
    7 min read
    Regression with Random Forest on Imbalanced data in R
    Random Forest is a versatile and powerful machine learning algorithm that can be used for regression tasks, especially when dealing with complex and nonlinear relationships in data. However, when the dataset is imbalanced — meaning one outcome class is significantly more frequent than the others — special considerations need to be taken to ensure t
    6 min read
    Imbalanced-Learn module in Python
    Imbalanced-Learn is a Python module that helps in balancing the datasets which are highly skewed or biased towards some classes. Thus, it helps in resampling the classes which are otherwise oversampled or undesampled. If there is a greater imbalance ratio, the output is biased to the class which has a higher number of examples. The following depend
    3 min read
    Bagging and Random Forest for Imbalanced Classification
    Ensemble learning techniques like bagging and random forests have gained prominence for their effectiveness in handling imbalanced classification problems. In this article, we will delve into these techniques and explore their applications in mitigating the impact of class imbalance. Classification problems are fundamental to machine learning and f
    8 min read
    Weighted Logistic Regression for Imbalanced Dataset
    In real-world datasets, it's common to encounter class imbalance, where one class significantly outnumbers the other(s). This class imbalance poses challenges for machine learning models, particularly for classification tasks, as models tend to be biased towards the majority class, leading to suboptimal performance. What are imbalanced datasets?Imb
    6 min read
    How to Handle Imbalanced Classes in Machine Learning
    In machine learning, "imbalanced classes" is a familiar problem particularly occurring in classification when we have datasets with an unequal ratio of data points in each class. Training of model becomes much trickier as typical accuracy is no longer a reliable metric for measuring the performance of the model. Now if the number of data points in
    15 min read
    What is Imbalanced Dataset
    In the realm of data science and machine learning, a common challenge that practitioners often encounter is dealing with imbalanced datasets. An Imbalanced Dataset refers to a situation where the number of instances across different classes in a classification problem is not evenly distributed. In simpler terms, one class has significantly more exa
    5 min read
    50 Best ChatGPT Prompts You Can't Miss in 2023
    ChatGPT, an innovative project from OpenAI initiated by Sam Altman, aims to revolutionize chatbot interactions by integrating advanced natural language processing capabilities. Working with ChatGPT is very easy if you know how to use it effectively. Moreover to teach you how to work effectively with ChatGPT here in the article we are going to list
    7 min read
    Jobs That ChatGPT Can Replace in Near Future
    As technology is evolving day by day so are artificial intelligence-powered tools, ChatGPT. It has created a heat in the world of technology and people are using it to do several tasks. Since, its release in November 2022, ChatGPT has been used for doing numerous impressive things like writing cover letters, emails, blogs, articles, and many other
    8 min read
    Handling Large data in Data Science
    Large data workflows refer to the process of working with and analyzing large datasets using the Pandas library in Python. Pandas is a popular library commonly used for data analysis and modification. However, when dealing with large datasets, standard Pandas procedures can become resource-intensive and inefficient. In this guide, we'll explore str
    5 min read
    Handling Categorical Data with Bokeh - Python
    As a data scientist, you will often come across datasets with categorical data. Categorical data is a type of data that can be divided into distinct categories or groups. For example, a dataset might have a column with the categories "red", "green", and "blue". Handling categorical data can be challenging because it cannot be processed in the same
    5 min read
    Handling Categorical Data in Python
    Categorical data is a set of predefined categories or groups an observation can fall into. Categorical data can be found everywhere. For instance, survey responses like marital status, profession, educational qualifications, etc. However, certain problems can arise with categorical data that must be dealt with before proceeding with any other task.
    6 min read
    Handling Inconsistent Data
    Handling inconsistent data in R is a crucial step in data preprocessing and cleaning. Inconsistent data can include missing values, outliers, errors, and inconsistencies in formats. In R Programming Language Properly addressing these issues ensures that your data is reliable and suitable for analysis. Here are common techniques for handling inconsi
    6 min read
    Handling Missing Values in Time Series Data
    Handling missing values in time series data in R is a crucial step in the data preprocessing phase. Time series data often contains gaps or missing observations due to various reasons such as sensor malfunctions, human errors, or other external factors. In R Programming Language dealing with missing values appropriately is essential to ensure the a
    5 min read
    Handling Missing Data in Decision Tree Models
    Decision trees, a popular and powerful tool in data science and machine learning, are adept at handling both regression and classification tasks. However, their performance can suffer due to missing or incomplete data, which is a frequent challenge in real-world datasets. This article delves into the intricacies of handling missing data in decision
    5 min read
    Handling Missing Data with IterativeImputer in Scikit-learn
    Handling missing data is a critical step in data preprocessing for machine learning projects. Missing values can significantly impact the performance of machine learning models if not addressed properly. One effective method for dealing with missing data is multivariate feature imputation using Scikit-learn's IterativeImputer. This article will del
    7 min read
    Handling Missing Data with KNN Imputer
    Missing data is a common issue in data analysis and machine learning, often leading to inaccurate models and biased results. One effective method for addressing this issue is the K-Nearest Neighbors (KNN) imputation technique. This article will delve into the technical aspects of KNN imputation, its implementation, advantages, and limitations. Tabl
    6 min read
    Is the R programming environment capable of handling unstructured data?
    Yes, the R programming environment is capable of handling unstructured data. R provides a wide range of packages and tools specifically designed for processing, analyzing, and visualizing unstructured data, such as text, images, and social media feeds. While traditionally known for its strengths in statistical analysis and structured data, R has ev
    2 min read
    Handling Overlapping Colorbar and Legends in Plotly
    Handling overlapping colorbars and legends in Plotly can be a common issue when creating complex visualizations. This article will explore various strategies to manage this problem effectively, ensuring your plots are both visually appealing and informative. We'll cover techniques using Plotly's layout customization options, focusing on Python impl
    3 min read
    Handling In-Memory and Large Datasets in CNTK
    Handling datasets in deep learning frameworks can be challenging, especially when dealing with large datasets that exceed the available memory. The Microsoft Cognitive Toolkit (CNTK) provides several mechanisms to handle both in-memory and large datasets effectively. This article explores how CNTK manages datasets of varying sizes, discusses best p
    8 min read
    Difference between Data Scientist, Data Engineer, Data Analyst
    In the world of big data and analytics, there are three key roles that are essential to any data-driven organization: data scientist, data engineer, and data analyst. While the job titles may sound similar, there are significant differences between the roles. In this article, we will explore the differences between data scientist, data engineer, an
    5 min read
    Handling missing values using Sunbird
    The Sunbird library is used for feature engineering purposes. In this library, you will get various techniques to handle missing values, outliers, categorical encoding, normalization and standardization, feature selection techniques, etc. Installation:pip install sunbirdHandling Missing Values: Datasets might have missing values, which can cause pr
    4 min read
    Handling categorical features with CatBoost
    Handling categorical features is an important aspect of building Machine Learning models because many real-world datasets contain non-numeric data which should be handled carefully to achieve good model performance. From this point of view, CatBoost is a powerful gradient-boosting library that is specifically designed for handling categorical featu
    10 min read
    Handling Categorical Features using LightGBM
    In the realm of machine learning and predictive modelling, the effective handling of categorical features is a vital task, often crucial to the success of a model. One popular and powerful tool that excels in this aspect is LightGBM. LightGBM is a gradient-boosting framework that not only delivers impressive predictive performance but also streamli
    10 min read
    Handling Missing Values with CatBoost
    Data is the cornerstone of any analytical or machine-learning endeavor. However, real-world datasets are not perfect yet and they often contain missing values which can lead to error in the training phase of any algorithm. Handling missing values is crucial because they can lead to biased or inaccurate results in data analyses and machine learning
    8 min read
    Handling Missing Values with Random Forest
    Data imputation is a critical challenge in machine learning, with missing values impacting statistical modelling. Random Forest, an ensemble learning method, is a robust solution for accurate predictions, particularly in healthcare. It can handle classification and regression problems, and it is more nuanced than traditional methods. It can handle
    10 min read