{"id":167191,"date":"2023-03-22T10:59:05","date_gmt":"2023-03-22T09:59:05","guid":{"rendered":"https:\/\/liora.io\/en\/?p=167191"},"modified":"2026-08-09T18:44:42","modified_gmt":"2026-08-09T17:44:42","slug":"management-of-unbalanced-classification-problems-i","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/management-of-unbalanced-classification-problems-i","title":{"rendered":"Managing Unbalanced Classification Problems :  Part 1"},"content":{"rendered":"\n<p><strong>Classification of unbalanced data is a <strong>classification problem<\/strong> where the training sample contains a strong disparity between the classes to be predicted. This problem is frequently encountered in <strong>binary classification problems<\/strong>, especially in anomaly detection.<\/strong><\/p>\n\n\n<p>This paper will be divided into <strong>two parts<\/strong>: The first one focuses on the choice of metrics specific to this type of data, and the second one details the range of useful methods to obtain a successful model.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"part-i-choosing-the-right-metrics\">Part I: Choosing the right metrics<\/h2>\n\n\n<h4 class=\"wp-block-heading\" id=\"what-is-an-evaluation-metric\">What is an evaluation metric?<\/h4>\n\n\n<p>An evaluation metric quantifies the performance of a predictive model. Choosing the right metric is almost mandatory when evaluating <a href=\"https:\/\/liora.io\/en\/machine-learning-what-is-it-and-why-does-it-change-the-world\"><strong>Machine Learning models<\/strong><\/a>, and the quality of a classification model depends directly on the metric used to evaluate it.<\/p>\n\n\n<p>About classification problems, metrics generally consist in<strong> comparing the real classes<\/strong> to the classes predicted by the model. They can also be used to interpret the predicted probabilities for these classes.<\/p>\n\n\n<p>One of the<strong> key performance<\/strong><strong>concepts<\/strong> for classification is the confusion matrix, which is a tabular visualization of the model predictions against the true labels. Each row of the confusion matrix represents the instances of a real class and each column represents the instances of a predicted class.<\/p>\n\n\n<p>Let&#8217;s take the example of a <strong>binary classification<\/strong>, where we have 100 positive instances and 70 negative instances.<\/p>\n\n\n<p>The confusion matrix below corresponds to the results obtained by our model:<\/p>\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Predicted classes\" decoding=\"async\" height=\"450\" loading=\"lazy\" sizes=\"(max-width: 800px) 100vw, 800px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/Predicted-classes-1-1024x576.jpg\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/Predicted-classes-1-1024x576.jpg 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/Predicted-classes-1-300x169.jpg 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/Predicted-classes-1-768x432.jpg 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/Predicted-classes-1-1536x864.jpg 1536w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/Predicted-classes-1.jpg 1920w\" style=\"width:100%;height:auto\" width=\"800\"\/><\/figure>\n\n\n<p>It gives an overview of the correct and false predictions.<\/p>\n\n\n<p>To summarize this matrix in a metric, it is possible to use the rate of good predictions or accuracy. Here it is equal to (90+57)\/170 = 0.86.<\/p>\n\n\n<p>The choice of an appropriate metric is <strong>not obvious<\/strong> for any Machine Learning model, but it is particularly difficult for unbalanced classification problems.<\/p>\n\n\n<p>In the case of data with a <strong>strong majority class<\/strong>, classical algorithms are often biased because their loss functions try to optimize metrics such as the rate of good predictions, without taking in account the data allocation.<\/p>\n\n\n<p>In the worst case, <strong>minority classes<\/strong> are treated as outliers of the majority class and the learning algorithm simply generates a trivial classifier that classifies each example into the majority class. The model will appear to perform well but this will only reflect the overrepresentation of the majority class. This is called <strong>paradoxical accuracy<\/strong>.<\/p>\n\n\n<p>In most cases, the minority class represents the <strong>greatest interest<\/strong> and that we would like to be able to identify, as in the example of fraud detection.<\/p>\n\n\n<p>The <strong>imbalance level varies<\/strong> but the use cases are recurrent: disease screening test , failure detection, search engine, spam filtering, marketing targeting&#8230;<\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"practical-application-churn-rate\">Practical application: Churn Rate<\/h4>\n\n\n<p>Let&#8217;s assume that a service company wants to predict its<strong> churn rate<\/strong>.<\/p>\n\n\n<p>Little reminder: the <strong>churn rate\u2019s ratio<\/strong> is the following : lost customers \/ total number of customers, measured over a given period, usually one year.<\/p>\n\n\n<p>The company wants to predict for each customer whether he will end his contract at the end of the year.<\/p>\n\n\n<p>We have a dataset containing personal information and contract characteristics of each customer of the company for the year X, as well as a variable to know if he renewed his contract at the end of the year.<\/p>\n\n\n<p>In our data, the number of &#8216;churners&#8217; corresponds to 11% of the total number of customers.<\/p>\n\n\n<p>We decide to train a <strong>first logistic regression model <\/strong>on our prepared and normalized data.<\/p>\n\n\n<p>Surprise! Our code displays a good prediction rate of 0.90!<\/p>\n\n\n<p>This is a very good score, but our goal is to<strong> predict<\/strong> the possible departure of customers. Does this result mean that out of 10 churners, 9 will be identified as such by the model? No!<\/p>\n\n\n<p>The only interpretation that can be made is that <strong>9 out of 10 customers<\/strong> have been well classified by the model.<\/p>\n\n\n<p>To successfully detect naive behavior in a model, the most effective tool is always the confusion matrix.<\/p>\n\n\n<p>A first look at the confusion matrix shows us that the good prediction rate obtained is largely influenced by the <strong>good behavior of the model <\/strong>on the dominant class (0).<\/p>\n\n\n<p>In order to evaluate the model related to the desired behavior on a class, it is possible to use a series of metrics from the confusion matrix, such as precision, recall, and f1-score, defined below.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:512px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"F1-score\" decoding=\"async\" height=\"89\" loading=\"lazy\" sizes=\"(max-width: 512px) 100vw, 512px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/image-3.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/image-3.png 512w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/image-3-300x52.png 300w\" style=\"width:512px;max-width:100%;height:auto\" width=\"512\"\/><\/figure>\n\n\n<p>Thus for a given class:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li aria-level=\"1\">A high precision and a high recall -&gt; The class has been\u00a0<strong>well managed<\/strong>\u00a0by the model<\/li>\n<li aria-level=\"1\">High precision and low recall -&gt; The\u00a0<strong>class is not well detected<\/strong>\u00a0but when it is, the model is very reliable.\u00a0<\/li>\n<li aria-level=\"1\">Low precision and high recall -&gt; The\u00a0<strong>class is well detected<\/strong>, but also includes observations of other classes.<\/li>\n<li aria-level=\"1\">Low precision and low recall -&gt; the<strong>\u00a0class has not been handled well\u00a0<\/strong>at all<\/li>\n<\/ul>\n\n\n<p>The F1 score measures both precision and recall.<\/p>\n\n\n<p>In the case of binary classification, the sensitivity and specificity correspond respectively to the recall of the positive and negative classes.<\/p>\n\n\n<p>Another metric, the<strong>\u00a0geometric mean (G-mean)<\/strong>, is useful for unbalanced classification problems: it is the root of the product of sensitivity and specificity.<\/p>\n\n\n<p>These different metrics are easily accessible thanks to the\u00a0<strong>imblearn package<\/strong>.<\/p>\n\n\n<p>The\u00a0<strong>classification_report_imbalanced()<\/strong>\u00a0function allows displaying a report containing the results on all the metrics of the package.<\/p>\n\n\n<p>We obtain the following table:<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\" style=\"width:512px;max-width:100%;margin-top:32px;margin-right:auto;margin-bottom:32px;margin-left:auto\"><img alt=\"Confusion matrix\" decoding=\"async\" height=\"288\" loading=\"lazy\" sizes=\"(max-width: 512px) 100vw, 512px\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/image-1.png\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/image-1.png 512w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2023\/03\/image-1-300x169.png 300w\" style=\"width:512px;max-width:100%;height:auto\" width=\"512\"\/><\/figure>\n\n\n<p>The table shows that the recall and f1-score for class 1 are bad, while for class 0 they are high. In addition, the geometric mean is also low.<\/p>\n\n\n<p>Thus, the trained model <strong>does not<\/strong> fit our data.<\/p>\n\n\n<p>In\u00a0<a href=\"https:\/\/liora.io\/en\/management-of-unbalanced-classification-problems-ii\" rel=\"noopener\" target=\"_blank\"><strong>Part II<\/strong><\/a>, we will discover methods that will allow us to obtain much better results.<\/p>\n\n\n<p>You want to improve your skills to build efficient and reliable models from unbalanced data sets?<strong><a href=\"\/en\/courses\/data-ai\/\"> Discover all our learning modules<\/a><\/strong>!<\/p>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"\/en\/courses\/data-ai\/machine-learning-engineer\">Start a Machine Learning Engineer training<\/a><\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Classification of unbalanced data is a classification problem where the training sample contains a strong disparity between the classes to be predicted. This problem is frequently encountered in binary classification problems, especially in anomaly detection. This paper will be divided into two parts: The first one focuses on the choice of metrics specific to this [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":167193,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2433],"class_list":["post-167191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/167191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=167191"}],"version-history":[{"count":4,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/167191\/revisions"}],"predecessor-version":[{"id":210900,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/167191\/revisions\/210900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/167193"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=167191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=167191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}