Imbalanced Data: How To Handle Imbalanced Classification Problems
Imbalanced Data: How To Handle Imbalanced Classification Problems
Imbalanced Data: How To Handle Imbalanced Classification Problems
Problems
C LA S S I F I C AT I O N I NT E RM E D I AT E M A C HI NE LE A RNI NG R S T RUC T URE D D AT A S UPE RVI S E D T E C HNI Q UE T E LE C O M
Introduction
If you have spent some time in machine learning and data science, you would have definitely come across
imbalanced class distribution. This is a scenario where the number of observations belonging to one class
is significantly lower than those belonging to the other classes.
This problem is predominant in scenarios where anomaly detection is crucial like electricity pilferage,
fraudulent transactions in banks, identification of rare diseases, etc. In this situation, the predictive model
developed using conventional machine learning algorithms could be biased and inaccurate.
This happens because Machine Learning Algorithms are usually designed to improve accuracy by reducing
the error. Thus, they do not take into account the class distribution / proportion or balance of classes.
This guide describes various approaches for solving such class imbalance problems using various
sampling techniques. We also weigh each technique for its pros and cons. Finally, I reveal an approach
using which you can create a balanced class distribution and apply ensemble learning technique designed
especially for this purpose.
Table of Content
One of the main challenges faced by the utility industry today is electricity theft. Electricity theft is the
third largest form of theft worldwide. Utility companies are increasingly turning towards advanced
analytics and machine learning algorithms to identify consumption patterns that indicate theft.
However, one of the biggest stumbling blocks is the humongous data and its distribution. Fraudulent
transactions are significantly lower than normal healthy transactions i.e. accounting it to around 1-2 % of
the total number of observations. The ask is to improve identification of the rare minority class as opposed
to achieving higher overall accuracy.
Machine Learning algorithms tend to produce unsatisfactory classifiers when faced with imbalanced
datasets. For any imbalanced data set, if the event to be predicted belongs to the minority class and the
event rate is less than 5%, it is usually referred to as a rare event.
Ex: In an utilities fraud detection data set you have the following data:
Fraudulent Observations = 20
Event Rate= 2 %
The main question faced during data analysis is – How to get a balanced dataset by getting a decent
number of samples for these anomalies given the rare occurrence for some them?
The conventional model evaluation methods do not accurately measure model performance when faced
with imbalanced datasets.
Standard classifier algorithms like Decision Tree and Logistic Regression have a bias towards classes
which have number of instances. They tend to only predict the majority class data. The features of the
minority class are treated as noise and are often ignored. Thus, there is a high probability of
misclassification of the minority class as compared to the majority class.
Evaluation of a classification algorithm performance is measured by the Confusion Matrix which contains
information about the actual and the predicted class.
Thus, to sum it up, while trying to resolve specific business challenges with imbalanced data sets, the
classifiers produced by standard machine learning algorithms might not give accurate results. Apart from
fraudulent transactions, other examples of a common business problem with imbalanced dataset are:
Datasets to identify customer churn where a vast majority of customers will continue using the service.
Specifically, Telecommunication companies where Churn Rate is lower than 2 %.
Data sets to identify rare diseases in medical diagnostics etc.
Natural Disaster like Earthquakes
Dataset used
In this article, we will illustrate the various techniques to train a model to perform well against highly
imbalanced datasets. And accurately predict rare events using the following fraud detection dataset:
Event Rate= 2 %
Dealing with imbalanced datasets entails strategies such as improving classification algorithms or
balancing classes in the training data (data preprocessing) before providing the data as input to the
machine learning algorithm. The later technique is preferred as it has wider application.
The main objective of balancing classes is to either increasing the frequency of the minority class or
decreasing the frequency of the majority class. This is done in order to obtain approximately the same
number of instances for both the classes. Let us look at a few resampling techniques:
Random Undersampling aims to balance class distribution by randomly eliminating majority class
examples. This is done until the majority and minority class instances are balanced out.
Event Rate= 2 %
In this case we are taking 10 % samples without replacement from Non Fraud instances. And combining
them with Fraud instances.
Event Rate for the new dataset after under sampling = 20/118 = 17%
Advantages
It can help improve run time and storage problems by reducing the number of training data
samples when the training data set is huge.
Disadvantages
It can discard potentially useful information which could be important for building rule classifiers.
The sample chosen by random under sampling may be a biased sample. And it will not be an
accurate representative of the population. Thereby, resulting in inaccurate results with the actual
test data set.
Over-Sampling increases the number of instances in the minority class by randomly replicating them in
order to present a higher representation of the minority class in the sample.
Event Rate= 2 %
Event Rate for the new data set after under sampling= 400/1380 = 29 %
Advantages
Unlike under sampling this method leads to no information loss.
Outperforms under sampling
Disadvantages
It increases the likelihood of overfitting since it replicates the minority class events.
In this case, the K-means clustering algorithm is independently applied to minority and majority class
instances. This is to identify clusters in the dataset. Subsequently, each cluster is oversampled such that
all clusters of the same class have an equal number of instances and all classes have the same size.
Event Rate= 2 %
After oversampling of each cluster, all clusters of the same class contain the same number of
observations.
Advantages
This clustering technique helps overcome the challenge between class imbalance. Where the
number of examples representing positive class differs from the number of examples representing
a negative class.
Also, overcome challenges within class imbalance, where a class is composed of different sub
clusters. And each sub cluster does not contain the same number of examples.
Disadvantages
The main drawback of this algorithm, like most oversampling techniques is the possibility of over-
fitting the training data.
This technique is followed to avoid overfitting which occurs when exact replicas of minority instances are
added to the main dataset. A subset of data is taken from the minority class as an example and then new
synthetic similar instances are created. These synthetic instances are then added to the original dataset.
The new dataset is used as a sample to train the classification models.
Fraudulent Observations = 20
Event Rate = 2 %
A sample of 15 instances is taken from the minority class and similar synthetic instances are generated 20
times
Advantages
Mitigates the problem of overfitting caused by random oversampling as synthetic examples are
generated rather than replication of instances
No loss of useful information
Disadvantages
While generating synthetic examples SMOTE does not take into consideration neighboring
examples from other classes. This can result in increase in overlapping of classes and can
introduce additional noise
SMOTE is not very effective for high dimensional data
It is a modified version of SMOTE. SMOTE does not consider the underlying distribution of the minority
class and latent noises in the dataset. To improve the performance of SMOTE a modified method MSMOTE
is used.
This algorithm classifies the samples of minority classes into 3 distinct groups – Security/Safe samples,
Border samples, and latent nose samples. This is done by calculating the distances among samples of the
minority class and samples of the training data.
Security samples are those data points which can improve the performance of a classifier. While on the
other hand, noise are the data points which can reduce the performance of the classifier. The ones which
are difficult to categorize into any of the two are classified as border samples.
While the basic flow of MSOMTE is the same as that of SMOTE (discussed in the previous section). In
MSMOTE the strategy of selecting nearest neighbors is different from SMOTE. The algorithm randomly
selects a data point from the k nearest neighbors for the security sample, selects the nearest neighbor
from the border samples and does nothing for latent noise.
The above section, deals with handling imbalanced data by resampling original data to provide balanced
classes. In this section, we are going to look at an alternate approach i.e. Modifying existing classification
algorithms to make them appropriate for imbalanced data sets.
The main objective of ensemble methodology is to improve the performance of single classifiers. The
approach involves constructing several two stage classifiers from the original data and then aggregate
their predictions.
2.2.1. Bagging Based techniques for imbalanced data
Bagging is used for reducing Overfitting in order to create strong learners for generating accurate
predictions. Unlike boosting, bagging allows replacement in the bootstrapped sample.
Event Rate= 2 %
There are 10 bootstrapped samples chosen from the population with replacement. Each sample contains
200 observations. And each sample is different from the original dataset but resembles the dataset in
distribution & variability.
The machine learning algorithms like logistic regression, neural networks, decision tree are fitted to each
bootstrapped sample of 200 observations. And the Classifiers c1, c2…c10 are aggregated to produce a
compound classifier. This ensemble methodology produces a stronger compound classifier since it
combines the results of individual classifiers to come up with an improved one.
Advantages
Improves stability & accuracy of machine learning algorithms
Reduces variance
Overcomes overfitting
Improved misclassification rate of the bagged classifier
In noisy data environments bagging outperforms boosting
Disadvantages
Bagging works only if the base classifiers are not bad to begin with. Bagging bad classifiers can
further degrade performance
Boosting is an ensemble technique to combine weak learners to create a strong learner that can make
accurate predictions. Boosting starts out with a base classifier / weak classifier that is prepared on the
training data.
The base learners / Classifiers are weak learners i.e. the prediction accuracy is only slightly better than
average. A classifier learning algorithm is said to be weak when small changes in data induce big changes
in the classification model.
In the next iteration, the new classifier focuses on or places more weight to those cases which were
incorrectly classified in the last round.
For a learned classifier to make strong predictions it should follow the following three conditions:
Each of the weak hypothesis has an accuracy slightly better than random guessing i.e. Error Term € (t)
should be slightly more than ½-β where β >0. This is the fundamental assumption of this boosting
algorithm which can produce a final hypothesis with a small error
After each round, it gives more focus to examples that are harder to classify. The quantity of focus is
measured by a weight, which initially is equal for all instances. After each iteration, the weights of
misclassified instances are increased and the weights of correctly classified instances are decreased.
For example in a data set containing 1000 observations out of which 20 are labelled fraudulent. Equal
weights W1 are assigned to all observations and the base classifier accurately classifies 400 observations.
Weight of each of the 600 misclassified observations is increased to w2 and weight of each of the
correctly classified observations is reduced to w3.
In each iteration, these updated weighted observations are fed to the weak classifier to improve its
performance. This process continues till the misclassification rate significantly decreases thereby
resulting in a strong classifier.
Advantages
1. Very Simple to implement
2. Good generalization- suited for any kind of classification problem ü Not prone to overfitting
Disadvantages
1. Sensitive to noisy data and outliers
In Gradient Boosting many models are trained sequentially. It is a numerical optimization algorithm where
each model minimizes the loss function, y = ax+b+e, using the Gradient Descent Method.
While both Adaboost and Gradient Boosting work on weak learners / classifiers. And try to boost them into
a strong learner, there are some fundamental differences in the two methodologies. Adaboost either
requires the users to specify a set of weak learners or randomly generates the weak learners before the
actual learning process. The weight of each learner is adjusted at every step depending on whether it
predicts a sample correctly.
On the other hand, Gradient Boosting builds the first learner on the training dataset to predict the samples,
calculates the loss (Difference between real value and output of the first learner). And use this loss to
build an improved learner in the second stage.
At every step, the residual of the loss function is calculated using the Gradient Descent Method and the
new residual becomes a target variable for the subsequent iteration.
Gradient Boosting can be done using the Gradient Boosting Node in SAS Miner and GBM package in R
For example: In a training data set containing 1000 observations out of which 20 are labelled fraudulent an
initial base classifier. Target Variable Fraud =1 for fraudulent transactions and Fraud=0 for not fraud
transactions.
For eg: Decision tree is fitted which accurately classifying only 5 observations as Fraudulent observations.
A differentiable loss function is calculated based on the difference between the actual output and the
predicted output of this step. The residual of the loss function is the target variable (F1) for the next
iteration.
Similarly, this algorithm internally calculates the loss function, updates the target at every stage and comes
up with an improved classifier as compared to the initial classifier.
Disadvantages
Gradient Boosted trees are harder to fit than random forests
Gradient Boosting Algorithms generally have 3 parameters which can be fine-tuned, Shrinkage
parameter, depth of the tree, the number of trees. Proper training of each of these parameters is
needed for a good fit. If parameters are not tuned correctly it may result in over-fitting.
XGBoost (Extreme Gradient Boosting) is an advanced and more efficient implementation of Gradient
Boosting Algorithm discussed in the previous section.
Extreme gradient boosting can be done using the XGBoost package in R and Python
The illustrative telecom churn dataset has 47241 client records with each record containing information
about 27 key predictor variables.
The data structure of the rare event data set is shown below post missing value removal, outlier treatment
and dimension reduction.
The unbalanced dataset is balanced using Synthetic Minority oversampling technique (SMOTE) which
attempts to balance the data set by creating synthetic instances. And train the balanced data set using
Gradient Boosting Algorithm as illustrated by the R codes in the next section
R Codes
#Load Data
rareevent_boost <- read.table("D:/Upasana/RareEvent/churn.txt",sep="|", header=TRUE) dmy<-
dummyVars("~.",data=rareevent_boost) rareeventTrsf<-data.frame(predict(dmy,newdata= rareevent_boost))
#Balance the Dataset using ubSMOTE# data<-ubBalance(X= input, Y=output, type="ubSMOTE", percOver=300,
#Write the balanced data to be used to train the model# write.table(balancedData,"D:/ Upasana/RareEvent
/balancedData.txt", sep="\t", row.names=FALSE)
#Build Boosting tree Model# repalceNAsWithMean <- function(x) {replace(x, is.na(x), mean(x[!is.na(x)]))}
training <- repalceNAsWithMean(training) testing <- repalceNAsWithMean(testing)
Results
This approach of balancing the data set with SMOTE and training a gradient boosting algorithm on the
balanced set significantly impacts the accuracy of the predictive model. By increasing its lift by around
20% and precision/hit ratio by 3-4 times as compared to normal analytical modeling techniques like logistic
regression and decision trees.
4. Conclusion
When faced with imbalanced data sets there is no one stop solution to improve the accuracy of the
prediction model. One may need to try out multiple methods to figure out the best-suited sampling
techniques for the dataset. In most cases, synthetic techniques like SMOTE and MSMOTE will outperform
the conventional oversampling and undersampling methods.
For better results, one can use synthetic sampling methods like SMOTE and MSMOTE along with advanced
boosting methods like Gradient boosting and XG Boost.
One of the advanced bagging techniques commonly used to counter the imbalanced dataset problem is
SMOTE bagging. It follows an entirely different approach from conventional bagging to create each
Bag/Bootstrap. It generates the positive instances by the SMOTE Algorithm by setting a SMOTE resampling
rate in each iteration. The set of negative instances is bootstrapped in each iteration.
Depending on the characteristics of the imbalanced data set, the most effective techniques will vary.
Relevant evaluation parameters should be considered during the model comparison.
While comparing multiple prediction models built through an exhaustive combination of the above-
mentioned techniques Lift & Area under the ROC Curve will be instrumental in determining which model is
superior to the others.
If you have any questions or doubts, feel free to drop them in the comments below.
References
1. Dmitry Pavlov, Alexey Gorodilov, Cliff Brunk “BagBoo: A Scalable Hybrid Bagging-theBoosting
Model”.2010
2. Fithria Siti Hanifah , Hari Wijayanto , Anang Kurnia “SMOTE Bagging Algorithm for Imbalanced Data Set
in Logistic Regression Analysis”. Applied Mathematical Sciences, Vol. 9, 2015
3. Lina Guzman, DIRECTV “Data sampling improvement by developing SMOTE technique in SAS” .Paper
3483-2015
4. Mikel Galar, Alberto Fern´andez, Edurne Barrenechea, Humberto Bustince and Francisco Herrera “A
Review on Ensembles for the Class Imbalance Problem: Bagging-, Boosting-, and Hybrid-Based
Approaches “ .2011 IEEE
Guest Blog