{"id":167218,"date":"2023-03-22T11:09:30","date_gmt":"2023-03-22T10:09:30","guid":{"rendered":"https:\/\/liora.io\/en\/?p=167218"},"modified":"2026-08-09T18:44:46","modified_gmt":"2026-08-09T17:44:46","slug":"management-of-unbalanced-classification-problems-ii","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/management-of-unbalanced-classification-problems-ii","title":{"rendered":"Managing Unbalanced Classification Problems :  Part 2"},"content":{"rendered":"\n<p><strong>This article will be divided into two parts: The first focuses on the choice of metrics specific to this type of data, the second details the range of useful methods to obtain a successful model.<\/strong><\/p>\n\n\n<p>After detailing the<strong> different problems related<\/strong> to data imbalance and demonstrating that the choice of the right performance metric is essential for the evaluation of our models, we will present a non-exhaustive list of useful techniques to fight against this type of problem.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"collect-more-data\">Collect more data<\/h2>\n\n\n<p>This may sound simplistic, but collecting more data is almost always <strong>overlooked<\/strong> and can sometimes be effective.<\/p>\n\n\n<p>Can you collect more data? Take a few minutes to think about collecting more data for your problem, it could potentially rebalance your classes to some degree.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"use-resampling-methods\">Use resampling methods<\/h2>\n\n\n<p>You can change the dataset you use before training your <strong>predictive model<\/strong> to have more balanced data.<\/p>\n\n\n<p>This strategy is called resampling and there are two main methods you can use to equalize the classes:<\/p>\n\n\n<p>Oversampling and Undersampling.<\/p>\n\n\n<p><strong>Oversampling methods<\/strong> work by increasing the <strong>number of observations<\/strong> of the minority class(es) in order to achieve a satisfactory ratio of minority class to majority class.<\/p>\n\n\n<p><strong>Undersampling methods<\/strong> work by decreasing the number of observations of the majority class(es) in order to reach a satisfactory ratio of minority class to majority class.<\/p>\n\n\n<p>These approaches are very easy to implement and quick to execute. They are a great starting point.<\/p>\n\n\n<p>Our advice: always <strong>try both approaches<\/strong> on all your unbalanced datasets, and check if it improves your chosen performance metrics.<\/p>\n\n\n<p><strong>Favor downsampling when you have large datasets<\/strong>: tens or hundreds of thousands of cases or more.<\/p>\n\n\n<p>Consider oversampling when you don&#8217;t have a lot of data: tens of thousands or less.<\/p>\n\n\n<p>Consider testing different class ratios. For example, you don&#8217;t have to aim for a 1:1 ratio in a binary classification problem, try other ratios.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"synthetic-sample-generation\">Synthetic sample generation<\/h2>\n\n\n<p>There are algorithms to generate synthetic samples automatically. The most popular of these algorithms is<strong> SMOTE<\/strong> (for Synthetic Minority Over-sampling Technique). As the name suggests, SMOTE is an oversampling method. It works by <strong>creating synthetic samples<\/strong> from the minority class instead of creating simple copies.<\/p>\n\n\n<p>To learn more about SMOTE, see <strong><a href=\"https:\/\/arxiv.org\/pdf\/1106.1813.pdf\">the original article<\/a><\/strong>.<\/p>\n\n\n<p>The<strong> ClusterCentroids algorithm<\/strong> is an Undersampling algorithm that uses Clustering methods to generate a number of centroids from the original data, in order to lose as little information as possible about the majority class, when it needs to be reduced.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"rethink-the-problem-find-an-other-way-to-solvre-the-problem\">Rethink the problem, find an other way to solvre the problem<\/h2>\n\n\n<p>Sometimes resampling methods are <strong>not efficient enough<\/strong>, and in this case, it is necessary to rethink the problem. It may be that the algorithm used is not suitable for your data.<\/p>\n\n\n<p>Do not hesitate to test other algorithms, possibly combined with the resampling methods seen above.<\/p>\n\n\n<p><strong>Tree-based ensemble models<\/strong> such as RandomForest are generally more suitable for unbalanced data.<\/p>\n\n\n<p>It is also possible to <strong>play with the probabilities<\/strong>. For example, if we want to be able to predict the vast majority of potential churners, even if it means misclassifying some <strong>non-churners<\/strong>, we can modify the probability threshold above which customers are considered as churners.<\/p>\n\n\n<p>The lower the threshold, the higher the precision of our class, but the recall will decrease.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"use-a-penalized-model\">Use a penalized model<\/h2>\n\n\n<p>Penalized classification <strong>imposes an additional cost<\/strong> on the model for classification errors made on the minority class during training. These penalties can bias the model to pay more attention to the minority class.<\/p>\n\n\n<p>In most classes of <strong>scikit-learn algorithms<\/strong>, it is possible to simply use the `class_weight` parameter. It allows penalizing errors made on a class by a new weight.<\/p>\n\n\n<p>The higher the weight of a class, the more errors in this class are penalized, and the more importance is given to it.<\/p>\n\n\n<p>The weights should be given in dictionary form, e.g. `{0:1, 1:5}`, to give 5 times the weight to errors made on class 1.<\/p>\n\n\n<p>The argument `&#8221;balanced&#8221;` allows us to associate with each class a weight inversely proportional to its frequency.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"use-methods-generating-subsampled-subsets\">Use methods generating subsampled subsets<\/h2>\n\n\n<p>Another solution proposed by the <strong>imblearn.ensemble<\/strong> module, is the use of classes containing ensemble models such as<strong> Boosting or Bagging<\/strong> which are trained at each step of the algorithm on a sample automatically rebalanced between the different classes.<\/p>\n\n\n<p>These model implementations make it possible to dispense with <strong>resampling methods<\/strong> before training and to apply them automatically to each selection of data by the algorithm.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"think-out-of-the-box-be-creative\">Think out of the box, be creative<\/h2>\n\n\n<p>You can test all of these techniques, combine them, or even think about <strong>relabeling the majority class data<\/strong> into subclasses to get a more balanced multi-class classification problem.<\/p>\n\n\n<p>In some cases, you can also think about using other <a href=\"https:\/\/liora.io\/en\/machine-learning-what-is-it-and-why-does-it-change-the-world\"><strong>Machine Learning methods<\/strong><\/a> like Anomaly Detection or Active Learning.<\/p>\n\n\n<p>We have presented many techniques, which you can choose from when working with this kind of data. Feel free to test these methods individually, and start with the simplest ones!<\/p>\n\n\n<p>Want to improve your skills in building powerful and reliable models from unbalanced data sets?<\/p>\n\n\n<p>Don&#8217;t hesitate to <a href=\"\/en\/appointment\"><strong>contact us<\/strong><\/a> for more information!<\/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=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-scientist\">Start a Training in Data Science<\/a><\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>This article will be divided into two parts: The first focuses on the choice of metrics specific to this type of data, the second details the range of useful methods to obtain a successful model. After detailing the different problems related to data imbalance and demonstrating that the choice of the right performance metric is [&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-167218","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\/167218","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=167218"}],"version-history":[{"count":5,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/167218\/revisions"}],"predecessor-version":[{"id":210901,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/167218\/revisions\/210901"}],"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=167218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=167218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}