<?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 Prit Manvar on Medium]]></title>
        <description><![CDATA[Stories by Prit Manvar on Medium]]></description>
        <link>https://medium.com/@pritmanvar?source=rss-14cca724c1b4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qkkQxWC03bd1qO8XjqKyag.jpeg</url>
            <title>Stories by Prit Manvar on Medium</title>
            <link>https://medium.com/@pritmanvar?source=rss-14cca724c1b4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 09:20:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pritmanvar/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[LightGBM Essentials: How it works and why it’s fast?]]></title>
            <link>https://medium.com/@pritmanvar/lightgbm-essentials-how-it-works-and-why-its-fast-586b83dda7af?source=rss-14cca724c1b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/586b83dda7af</guid>
            <category><![CDATA[gradient-boosting]]></category>
            <category><![CDATA[ensemble-techniques]]></category>
            <category><![CDATA[boosting-algorithm]]></category>
            <category><![CDATA[lightgbm]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Prit Manvar]]></dc:creator>
            <pubDate>Sat, 02 Mar 2024 12:01:00 GMT</pubDate>
            <atom:updated>2024-03-04T04:34:00.058Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Banner for lightGBM" src="https://cdn-images-1.medium.com/max/1024/0*VO7JUVUY2qvu4l27" /></figure><h3>Table of content</h3><ul><li><a href="#69c8">What is LightGBM</a></li><li><a href="#198d">Why is Light Boost fast?</a><br> — <a href="#5084">Histogram-based algorithm</a><br> — <a href="#1d84">Leaf wise tree growth</a><br> — <a href="#85a3">Gradient-based One Side Sampling</a><br> — <a href="#39ac">Exclusive Feature Bundling</a></li><li><a href="#a493">How LightGBM Works?</a></li><li><a href="#7cb4">Overall Comparison</a></li><li><a href="#1e2c">Reference</a></li></ul><h3>What is LightGBM</h3><p>LightGBM (Light Gradient Boosting Machine) is a <strong>Gradient Boosting</strong> algorithm. Boosting algorithms are widely used machine learning algorithms, due to their <strong>efficiency, accuracy and interpretability</strong>. But with the emergence of big data, many gradient boosting algorithms are facing <strong>new challenges</strong>, especially in the <strong>tradeoff between accuracy and efficiency</strong>. So, <strong>Light Boost</strong> is an algorithm which gives you <strong>speed with high accuracy</strong>.</p><h3>Why is LightGBM fast?</h3><p><strong>Conventional implementations</strong> of Gradient Boosting Decision Trees(GBDT) need to, for every feature, <strong>scan all the data instances</strong> to estimate the <a href="https://en.wikipedia.org/wiki/Information_gain_(decision_tree)#:~:text=Information%20gain%20is%20the%20basic%20criterion%20to%20decide%20whether%20a%20feature%20should%20be%20used%20to%20split%20a%20node%20or%20not.%20The%20feature%20with%20the%20optimal%20split%20i.e.%2C%20the%20highest%20value%20of%20information%20gain%20at%20a%20node%20of%20a%20decision%20tree%20is%20used%20as%20the%20feature%20for%20splitting%20the%20node.">information gain</a> of all the possible split points. Because of that their computational <strong>complexity will be proportional</strong> to both the <strong>number of features</strong> and the <strong>number of instances</strong>. This makes these implementations very time consuming when handling big data.</p><p>To tackle this problem, a simple idea is to <strong>reduce the number of data instances</strong> and the <strong>number of features</strong>. So the challenge is to perform sampling in a way that doesn’t impact accuracy.</p><p>LightGBM achieves it by implementing the following techniques.</p><ol><li><a href="#5084">Histogram-based algorithm</a></li><li><a href="#1d84">Leaf wise tree growth</a></li><li><a href="#85a3">Gradient-based One Side Sampling</a></li><li><a href="#39ac">Exclusive Feature Bundling</a></li></ol><h4>Histogram-based algorithm</h4><p>The main cost of GBDT lies in learning decision trees. And the most time-consuming part of learning a decision tree is to find the best split points. One possible way to find th best split is to enumerate all possible split points and find the best one. But it is very inefficient in both training speed and memory consumption.</p><p>So, another popular algorithm is the histogram-based algorithm. In the histogram-based algorithm, Instead of finding the split points on every possible split, we bucket continuous values into different bins and use these bins to construct feature histograms during training.</p><p>We can handle the number of bins by <a href="https://lightgbm.readthedocs.io/en/stable/Parameters.html#max_bin">max_bin</a>, <a href="https://lightgbm.readthedocs.io/en/stable/Parameters.html#max_bin_by_feature">max_bin_my_feature</a>, <a href="https://lightgbm.readthedocs.io/en/stable/Parameters.html#min_data_in_bin">min_data_in_bin</a> and <a href="https://lightgbm.readthedocs.io/en/stable/Parameters.html#bin_construct_sample_cnt">bin_construct_sample_cnt</a></p><figure><img alt="Demonstration of histogram-based split" src="https://cdn-images-1.medium.com/max/913/1*e8Qtbls_oxVvDxDvCyqlhA.png" /><figcaption>Difference between number of splits in histogram-based algorithm.</figcaption></figure><h4>Leaf wise tree growth</h4><p>In LightGBM, growth of the tree is not same as in traditional GBDTs. Instead of considering each node equal, we focus on the ones promising the biggest improvement. That’s the core idea of LightGBM’s leaf wise tree growth strategy.</p><ul><li><strong>Greedy Growth</strong>: Unlike traditional methods that consider all leaves at a level, LightGBM finds the leaf that will most improve the model’s accuracy and only splits that one. Think of it as pruning out unnecessary branches, focusing on the most fruitful path.</li><li><strong>Asymmetrical Trees</strong>: This targeted approach can lead to asymmetrical trees, where branches develop unevenly, with some parts growing deeper than others. It’s not about perfection, but about maximizing accuracy with fewer resources.</li></ul><figure><img alt="Level wise tree growth in traditional GBDT" src="https://cdn-images-1.medium.com/max/596/0*YNlbi715q3XVT8k-.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*SCq2A9pZ3EKXIQZRa-kPlw.png" /></figure><p><strong>Benefits of leaf-wise tree growth:</strong></p><ol><li><strong>Speed</strong>: Leaf-wise tree growth focuses on the most impactful splits. Which makes LightGBM incredibly fast. Also by this method LightGBM avoids expanding subtrees which doesn’t impact more on model.</li><li><strong>Accuracy: </strong>Leaf-wise growth often leads to lower loss and higher accuracy by prioritizing the most informative regions.</li></ol><h4>Gradient-basedOverall Comparison One Side Sampling (GOSS)</h4><p>Gradient means a change in value. In LightGBM, a gradient is a change in loss. In our case, the gradient is derivative of a loss function and a loss function can be any differentiable function. The higher gradient means higher error.</p><blockquote><strong><em>What does gradient do?<br></em></strong><em>Think of a classroom where every student gets the same attention, even the ones who have a good score in every exam. </em><strong><em>B</em></strong><em>ut in this case, is it necessary to give the same attention to every student? Not really, right? We should focus more on students who are weak instead of treating everyone equally for better all-over performance. The same problem is faced by traditional Gradient Boosting Decision Tree (GBDT) algorithms also. They train each new tree on every single data point, even if they are already well-understood by the algorithm. That’s where GOSS cames into the picture.</em></blockquote><p>With the help of GOSS we can focus on points with high prediction error and those points should be primary target to fit in the next tree.</p><p>GOSS not only allows us to focus on points with high prediction error but also it reduces the number of samples by removing points with low prediction error. So our algorithm becomes faster.</p><p>But when we discard the data with small a gradient, the data distribution will be changed, which will hurt the accuracy of the model. To avoid this problem GOSS keeps all points with large gradients and performs random sampling on points with low gradients. In order to compensate the infulence to the data distribution GOSS introduces a constant multiplier for the data points with small gradients.</p><blockquote><strong>Algorithm for gradient-based one side sampling.</strong></blockquote><pre>Inputs:<br>I -&gt; Traning data<br>d -&gt; Number of iterations<br>a -&gt; Sampling ratio of large gradient data<br>b -&gt; Sampling ratio of small gradient data<br>loss -&gt; loss function<br>L -&gt; week learner<br><br><br>models = {} #List of models, initially empty list<br>topN = a*len(I) # number of top a% points.<br>randN = b*len(I) # number of random b% points. <br>fact = (1-a)/b <br><br>for i = 1 to d do<br>  preds = models.predict(I)<br>  g = loss(I, preds) # calculate gradients for all training points<br>  w = {1,1,1,...} # Initially assign weight 1 to every point.<br>  sorted_gradients = GetSorted(abs(g)) # sorts gradients based on absolute value<br>  topSet = sorted[1:topN]<br>  randomSet = RandomPick(sorted[topN:len(I)], randN] # pick random randN points from remaining points.<br>  usedSet = topSet + randomSet # Create final combined set which we will use now<br>  w[randSet] *= fact # Amplify weights of random set by fact.<br><br>  newModel = L(I[usedSet], -g[usedSet], w[usedSet]) # give used traning set, gradients and weights of used set to weak learner and get new model<br>  models.append(newModel)</pre><p>In this algorithm, GOSS firstly sorts the data points according to the absolute value of their gradients and selects the top a×100% instances. Then it randomly samples b×100% instances from the rest of the data. After that, GOSS amplifies the sampled data with small gradients by a constant (1−a)/b when calculating information gain. By doing so, we put more focus on the under-trained instances without changing the original data distribution by much. And then it passes this data to next weak learner.</p><h4>Exclusive Feature Bundling</h4><p>Usually in real applications when we are dealing with high dimensional data there are a large number of features that are mutually exclusive. Specifically, in a sparse feature space, many features are (almost) exclusive, i.e., they rarely take nonzero values simultaneously. Examples include one-hot features (e.g., one-hot word representation in text mining). We can safely bundle such exclusive features. By doing it LightGBM is able to reduce number of features without affecting accuracy so it can run efficiently.</p><p>How EFB Works:<br>1. Identify mutually exclusive feature bundlesfrom training set.<br>2. Merge the feature bundle and assign a value to the bundle.</p><h3>How LightGBM Works?</h3><p>LightGBM fistly does some preprocessing like creating <strong>bins for histograms</strong> and <strong>Exclusive Feature Bundling</strong>.</p><p>After doing preprocessing, it will find variance gain for each leaf node and find the best node to split which gives big improvement.</p><p>Equation for finding variance gain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yBZMC_H0ubLqxhBI8no08w.png" /></figure><p>Where,</p><ul><li>d = splitting criteria (ie: value of point is &gt; d or &lt; d)</li><li>j = feature on which we are applying splitting</li><li>n = number of total points</li><li>a = sampling ratio for large gradient data</li><li>b = sampling ratio for small gradient data</li><li>Al = Includes all points which are less than or equal to d and It has high gradient (ie: falls into top a%)</li><li>gi = gradient for ith point.</li><li>Bl = Includes all points which are less than or equal to d and It has low gradient (ie: falls into random b%)</li><li>nl = number of points in Al + number of points in Bl</li><li>Ar = Includes all points which are more than d and It has high gradient (ie: falls into top a%)</li><li>Br = Includes all points which aremore than d and It has low gradient (ie: falls into random b%)</li><li>nl = number of points in Ar + number of points in Br</li></ul><p>LightGBM will calculate variance gain for each feature in node i and select the best feature to split which gives the highest variance gain. After selecting a feature which gives the highest variance gain for each leaf it will compare maximum possible variance gain in all leaf nodes and split the leaf node with the highest variance gain.</p><p>We will repeat this leaf splitting process until we get desired result and then calculate residuals and create new input set based on this residuals and predicted value and pass this details to next tree.</p><p>At last, we will combine the output of all trees just like we do in traditional gradient boosting decision tree algorithms.</p><h3>Overall Comparison</h3><p>Here, I am adding overall comparison of XGBoost and LightGBM from research paper of LightGBM.</p><figure><img alt="Overall comparision of XGBoost and LightGBM" src="https://cdn-images-1.medium.com/max/850/1*f3pBNUqHeCUdr7rOl7xtJg.png" /><figcaption>Overall comparision of XGBoost and LightGBM</figcaption></figure><h3>Reference</h3><ul><li><a href="https://proceedings.neurips.cc/paper_files/paper/2017/file/6449f44a102fde848669bdd9eb6b76fa-Paper.pdf">https://proceedings.neurips.cc/paper_files/paper/2017/file/6449f44a102fde848669bdd9eb6b76fa-Paper.pdf</a></li><li><a href="https://lightgbm.readthedocs.io/en/stable/index.html">https://lightgbm.readthedocs.io/en/stable/index.html</a></li><li><a href="https://github.com/Microsoft/LightGBM/blob/master/docs/Features.rst">https://github.com/Microsoft/LightGBM/blob/master/docs/Features.rst</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=586b83dda7af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stemming and lemmatization — A text preprocessing techniques.]]></title>
            <link>https://medium.com/@pritmanvar/stemming-and-lemmatization-a-text-preprocessing-techniques-f1ed915cfec8?source=rss-14cca724c1b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1ed915cfec8</guid>
            <category><![CDATA[text-preprocessing]]></category>
            <category><![CDATA[nlp-text-preprocessing]]></category>
            <category><![CDATA[stemming]]></category>
            <category><![CDATA[lemmatization]]></category>
            <category><![CDATA[lemmatization-vs-stemming]]></category>
            <dc:creator><![CDATA[Prit Manvar]]></dc:creator>
            <pubDate>Sun, 25 Feb 2024 12:44:51 GMT</pubDate>
            <atom:updated>2024-02-28T09:48:53.369Z</atom:updated>
            <content:encoded><![CDATA[<h3>Stemming and lemmatization, A text preprocessing techniques.</h3><figure><img alt="Word cloud for stemming and lemmatization." src="https://cdn-images-1.medium.com/max/1024/1*Vth3Vby9K1p706U9hgyxNg.png" /></figure><h3>Table of content</h3><ul><li>Introduction</li><li>Stemming<br>- How stemming work?<br>- Apply stemming using NLTK<br>- Advantages of stemming<br>- Limitations of stemming</li><li>Lemmatization<br>- How lemmatization work?<br>- Apply lemmatization using NLTK<br>- Advantages of lemmatization<br>- Limitations of lemmatization</li><li>Which one to use between stemming and lemmatization.</li></ul><h3>Introduction.</h3><p>In Natural Language Processing (NLP), Stemming and Lemmatization are great techniques for <strong>text preprocessing</strong>. This techniques converts <strong>inflected forms of variants</strong> of words to <strong>common root words</strong> accross text. For example words like Change, Changing, Changes will get converted to chang or change based on the algorithm you choose.</p><p><strong><em>But why do we need to convert inflected forms to common forms?</em><br></strong>Let’s start with some other basic questions. When we get words like walk, walked, walks or walking, <strong>Do these words have different meanings?</strong> <strong>No</strong>, at the end of the day, they all have the same kind of meaning. That’s why we want to consider it as a single word. But if we pass these words to the machine as it is then obviously it will consider then different. Just because of that firstly we want to convert all inflected forms to a common form.</p><p>To do so we have mainly two techniques: 1. Stemming and 2. Lemmatization. Let’s learn about them in detail.</p><h3>Stemming</h3><p>It is a very effective text preprocessing technique when <strong>time is a very crucial</strong> parameter and you <strong>don’t need a valid word</strong>. Stemming is the process of reducing the inflected form of a word to one of the so-called “Stem”, or root forms. Its primary goal is to reduce words to their base or root form and group words with similar meanings or roots together even if they have different inflections, prefixes or suffixes, so it is <strong>not necessary</strong> that the stem will be a <strong>valid word</strong>.</p><p>Example:</p><figure><img alt="Image which describe that stem for change, changing, changes, changed or changer will be chang." src="https://cdn-images-1.medium.com/max/362/1*igHZta1PDrj3naS6RdaRTg.png" /></figure><h4>How does stemming work?</h4><p>The process of stemming mainly focuses on removing prefixes or suffixes from the word to get stems. So it will look for any prefix or suffix in the word and return a word without any suffix or prefix. It does not try to come up with a valid word and just removes any prefix or suffix. it is faster than lemmatization.</p><h4>Apply stemming using NLTK</h4><pre>from nltk.stem.snowball import SnowballStemmer<br><br>stemmer = SnowballStemmer(&quot;english&quot;)<br><br>words = [&#39;caresses&#39;, &#39;flies&#39;, &#39;dies&#39;, &#39;mules&#39;, &#39;denied&#39;, &#39;died&#39;, &#39;agreed&#39;, &#39;owned&#39;, &#39;humbled&#39;, &#39;sized&#39;, &#39;meeting&#39;, &#39;stating&#39;, &#39;siezi<br>ng&#39;, &#39;itemization&#39;, &#39;sensational&#39;, &#39;traditional&#39;, &#39;reference&#39;, &#39;colonizer&#39;, &#39;plotted&#39;]<br><br>singles = [stemmer.stem(word) for word in words]<br><br>print(&#39; &#39;.join(singles))<br># Output: caress fli die mule deni die agre own humbl size meet state siez item sensat tradit refer colon plot</pre><h4>Advantages of stemming</h4><ul><li><strong>Improved model performance<br></strong>Stemming reduces the number of unique words that need to be processed by an algorithm, which can improve its performance. Additionally, it can also make the algorithm run faster and more efficiently because it just removes the prefix and suffix from the word.</li><li><strong>Grouping similar words</strong><br>Words with similar meanings can be grouped together, even if they have distinct forms. This can be a useful technique in tasks such as document classification or in other tasks where it’s important to identify the key topic.</li><li><strong>Easy to analyze and understand<br></strong>Since stemming typically reduces the size of the vocabulary, it’s much easier to analyze, compare and understand texts. This is helpful in tasks such as sentiment analysis, where the goal is to determine the sentiment of the document.</li></ul><h4>Limitations of stemming</h4><ul><li><strong>Overstemming / False positives<br></strong>Stemming might reduce different words to the same word just because they looks similar even if they are not. For example, the stemmer algorithm might reduce “universal”, “university” and “universe” to the same word “univers”.</li><li><strong>Understemming / False negative<br></strong>Stemming might reduce two words to different even if they have the same kind of meaning. For example, the stemming algorithm will reduce “eat” and “ate” to different words “eat” and “ate” even if both have the same kind of meaning.</li><li><strong>Language challenges<br></strong>Design of stemmers can be more complex based on the language’s morphology, spelling and character encoding. Like Italian stemmer is more complicated than English stemmer because there is a higher number of verb inflections.</li><li><strong>Loss of meaning<br></strong>Stemming algorithms may or may not produce words with meaning so in the process of stemming we might lose the exact meaning of the words. Hence we will not be able to find the actual meaning of the sentence.</li></ul><h3>Lemmatization</h3><p>Lemmatization can be a very useful technique when you want a <strong>valid word</strong> in return. It is a process that involves reducing <strong>words to their base or root</strong> form, known as the “<strong>lemma”</strong>. But unlike stemming, <strong>lemmatization will always give valid words.</strong></p><p>Example:</p><figure><img alt="Image which describe that lemma for change, changing, changes, changed or changer will be change." src="https://cdn-images-1.medium.com/max/362/1*sp00MMKc67j7Bq263kvxnw.png" /></figure><h4>How does lemmatization work?</h4><p>Let’s know about POS Tagging before getting into the working of lemmatization.</p><p><strong>POS Tagging: </strong>Part-of-speech tagging involves <strong>assigning a grammatical category</strong> (like a noun, verb, adjective, etc.) to each token. Lemmatization often relies on this information, as the base form of a word can depend on its grammatical role in a sentence.</p><p>In the process of lemmatization, firstly we will <strong>assign a part of speech</strong> to each word. Once each word has been tokenized and assigned a part of speech tag, the lemmatization algorithm uses a <strong>lexicon or linguistic rule</strong> to determine the lemma for each word. The lemma is the base form of the word, which may not necessarily be the same as the word’s root. For example, the lemma for “running” is “run” and the lemma for “better” (in the context of an adjective) is “good”.</p><p>Lemmatization is distinct from stemming. While <strong>stemming involves chopping of prefixes or suffixes</strong> from words to obtain a common root, <strong>lemmatization aims for a valid base</strong> form through <strong>linguistic analysis</strong>.</p><h4>Apply lemmatization using NLTK</h4><pre>from nltk.stem import WordNetLemmatizer<br><br>wnl = WordNetLemmatizer()<br><br>words = [&#39;rocks&#39;, &#39;runs&#39;, &#39;running&#39;, &#39;go&#39;, &#39;going&#39;, &#39;goes&#39;, &#39;ate&#39;, &#39;eat&#39;]<br>after_lemmatization = [wnl.lemmatize(word, pos=&#39;v&#39;) for word in words]<br><br>print(after_lemmatization)<br># Output: [&#39;rock&#39;, &#39;run&#39;, &#39;run&#39;, &#39;go&#39;, &#39;go&#39;, &#39;go&#39;, &#39;eat&#39;, &#39;eat&#39;]</pre><h4>Advantages of lemmatization</h4><ul><li><strong>Accuracy<br></strong>Lemmatization does not cut words off as we see in stemming algorithms. Analysis of words is conducted based on the word’s POS to consider the context when producing lemmas. Also, lemmatization leads to real dictionary words being produced.</li></ul><h4>Limitations of lemmatization</h4><ul><li><strong>Time-consuming<br></strong>If we compare lemmatization with stemming, then lemmatization is a slower and time-consuming process. This is because lemmatization involves performing morphological analysis and deriving the meaning of words from a dictionary.</li></ul><h3>Which one to use between stemming and lemmatization?</h3><p>The selection between stemming and lemmatization can totally depend on the use case but based on the nature of both I can specify some use cases where we can go with stemming or lemmatization.</p><h4>When can we use stemming?</h4><p>We can use stemming in such cases where we need speed but we don’t care about the meaning of the word but can’t compromise with time and memory. We just want to group similar words.</p><ul><li><strong>Spam Detection</strong></li><li><strong>Document Categorization</strong></li><li><strong>Social Media Content Analysis</strong></li><li><strong>Sentiment Analysis</strong></li></ul><h4>When can we use lemmatization?</h4><p>We can use lemmatization in such cases where we must preserve the meaning of the word and we can compromise with time and memory.</p><ul><li><strong>Chatbots</strong></li><li><strong>Part-of-Speech Tagging</strong></li><li><strong>Text summarization</strong></li><li><strong>Language translation.</strong></li><li><strong>Speech to text</strong></li></ul><p>I hope it helped!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1ed915cfec8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>