<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Suryakant Agrawal on Medium]]></title>
        <description><![CDATA[Stories by Suryakant Agrawal on Medium]]></description>
        <link>https://medium.com/@suryaa62?source=rss-9b9e9cbf320a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Suryakant Agrawal on Medium</title>
            <link>https://medium.com/@suryaa62?source=rss-9b9e9cbf320a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 06:28:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suryaa62/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Balancing your imbalanced data]]></title>
            <link>https://medium.com/student-technical-community-vit-vellore/balancing-your-imbalanced-data-afb6edde0368?source=rss-9b9e9cbf320a------2</link>
            <guid isPermaLink="false">https://medium.com/p/afb6edde0368</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-cleaning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[imbalanced-dataset]]></category>
            <category><![CDATA[imbalanced-data]]></category>
            <dc:creator><![CDATA[Suryakant Agrawal]]></dc:creator>
            <pubDate>Fri, 25 Jun 2021 12:37:37 GMT</pubDate>
            <atom:updated>2021-06-25T12:37:37.620Z</atom:updated>
            <content:encoded><![CDATA[<p>Nothing is good in nature if it is more than the required quantity. A higher amount of CO₂ will warm the planet, a higher amount of rain will flood the cities, a higher amount of thinking will ruin your execution. The same applies to our Data. A higher number of instances of one class will lead to bogus predictions. You can understand this with a simple example — imagine that I have a bag full of red balls and one blue ball and I ask you what color ball will I draw? If you have a love of probability like me you will say red ball because it&#39;s in abundance and hence it is the logical choice. Now imagine you are a machine learning model whose job is to predict if a patient has cancer or not. Clearly, the logical choice is to conclude that the patient has no cancer because most of them don’t. In fact, you as a model will get high accuracy by just marking everyone cancer-free. But do you see the problem here?</p><p>Two possible conclusions can be made from the above example if our data is imbalanced — the first being that our model will tend to predict the class which is in high number regardless of the other parameters. Secondly, model accuracy will not show us the true picture, because if you have a dataset of 100 records with 99 instances of class 1, your model will get 99 percent accuracy by just predicting every instance as class 1.</p><p>Now the question is, how can we remove this imbalance from our data? Let us first understand what we mean by balancing our data. Our data will be balanced if there is an equal number of instances of every class. I will discuss three methods here to do the same-</p><ol><li>Random Under-sampling the majority class.</li><li>Over-sampling the minority class using SMOTE.</li><li>Ensemble method.</li></ol><p>We will use the <a href="https://imbalanced-learn.org/stable/install.html"><strong><em>Imblearn</em></strong></a> library to implement these techniques. Imblearn has a similar API like <a href="https://scikit-learn.org/"><strong><em>Scikit-learn</em></strong></a> — one fit method and one resample method(and a shortcut method <em>fit_resample</em>).</p><p>To install imblearn on your computer using pip you can use the following command in your terminal.</p><blockquote>pip install imblearn</blockquote><h3>Under-sampling</h3><p>In this method, we reduce the number of instances of the majority class to make them equal to the minority class. One simple way to do this is by randomly selecting the instances of the majority class. For example, if you have 900 instances of the majority class and 400 instances of the minority class, we can under-sample the majority class by randomly selecting 400 instances of the majority class. We can do this using imblearn’s <a href="https://imbalanced-learn.org/stable/references/generated/imblearn.under_sampling.RandomUnderSampler.html#imblearn.under_sampling.RandomUnderSampler"><strong><em>RandomUnderSampler</em></strong></a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c394e67ec8ea637a242c8f079942256e/href">https://medium.com/media/c394e67ec8ea637a242c8f079942256e/href</a></iframe><p>You will use <em>x_resampled</em> and <em>y_resampled</em> in your model(<em>X</em>, <em>y </em>are your original data). The parameter <em>random_state </em>is<em> </em>used to generate seed so that you get the same result every time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*ZSJSXA85dFhbZIQkLRNt9Q.png" /></figure><h3>Over-sampling</h3><p>Another way to balance our data is by creating more instances of the minority class. The easiest way to accomplish this is by copying random instances of the minority classes. Imblearn provides us <a href="https://imbalanced-learn.org/stable/references/generated/imblearn.over_sampling.RandomOverSampler.html#imblearn.over_sampling.RandomOverSampler"><strong><em>RandomOverSampler</em></strong></a> for the same. If repeating samples is an issue, <strong><em>RandomOverSampler</em></strong> has a parameter shrinkage to offset the data a little bit. To use shrinkage your data must be numerical.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc69f6d81a33eff785b5022f4c9d0129/href">https://medium.com/media/bc69f6d81a33eff785b5022f4c9d0129/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*8wAn1PEvt3cjfCYWZosSOg.png" /></figure><p>Apart from this, there is one more popular technique to oversample your data — <a href="https://imbalanced-learn.org/stable/references/generated/imblearn.over_sampling.SMOTE.html#imblearn.over_sampling.SMOTE"><strong><em>The Synthetic Minority Oversampling Technique or SMOTE</em></strong></a>. While <strong><em>RandomOverSampler</em></strong> is over-sampling by duplicating some of the original samples of the minority class, <strong><em>SMOTE</em></strong> generates new samples by interpolation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a8e6640ac51b64ca159d7c9ca5fa532/href">https://medium.com/media/3a8e6640ac51b64ca159d7c9ca5fa532/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*r9Q8fb7cWktwUN9eZbLP5g.png" /></figure><p>One issue with <strong><em>SMOTE</em></strong> is that you should only have numerical data. If you have only categorical data then you can use the <a href="https://imbalanced-learn.org/stable/references/generated/imblearn.over_sampling.SMOTEN.html#imblearn.over_sampling.SMOTEN"><strong><em>SMOTEN</em></strong></a> variant. If you have a mix of categorical and numerical data then you can use the <a href="https://imbalanced-learn.org/stable/references/generated/imblearn.over_sampling.SMOTENC.html#imblearn.over_sampling.SMOTENC"><strong><em>SMOTENC</em></strong></a> variant. <strong><em>SMOTENC</em></strong> gets the indices of categorical features via the <em>categorical_features</em> parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/833eea1f643dd0f13cf9fb6cba1923c0/href">https://medium.com/media/833eea1f643dd0f13cf9fb6cba1923c0/href</a></iframe><h3>Ensemble Method</h3><p>To understand this technique, suppose you have data with the majority class having 3000 instances and the minority class having 1000 instances. Now we can create say, 3 different models, each having 1000 different instances of the majority class and the same 1000 instances of the minority class. At the time of prediction, we will predict by voting i.e. if model 1 predicts class 0 and model 2 and model 3 predicts class 1, then our final prediction will be 1.</p><p>Imblearn provides us with <a href="https://imbalanced-learn.org/stable/references/generated/imblearn.ensemble.BalancedBaggingClassifier.html#imblearn.ensemble.BalancedBaggingClassifier"><strong><em>BalancedBaggingClassifier</em></strong></a> which takes the <em>scikit-learn’s</em> estimator as a parameter. you can also decide your desired sampling strategy, number of estimators, etc.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f407f20d93ed90841793eb89c923d4f5/href">https://medium.com/media/f407f20d93ed90841793eb89c923d4f5/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afb6edde0368" width="1" height="1" alt=""><hr><p><a href="https://medium.com/student-technical-community-vit-vellore/balancing-your-imbalanced-data-afb6edde0368">Balancing your imbalanced data</a> was originally published in <a href="https://medium.com/student-technical-community-vit-vellore">Student Technical Community — VIT Vellore</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>