<?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 Selim Hacıömeroğlu on Medium]]></title>
        <description><![CDATA[Stories by Selim Hacıömeroğlu on Medium]]></description>
        <link>https://medium.com/@selimho02?source=rss-33a6a581f150------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ud_F5E5ipI8tCvT0h0_xXA.png</url>
            <title>Stories by Selim Hacıömeroğlu on Medium</title>
            <link>https://medium.com/@selimho02?source=rss-33a6a581f150------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 17:47:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@selimho02/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 Explained]]></title>
            <link>https://medium.com/@selimho02/lightgbm-explained-1f38523d30c0?source=rss-33a6a581f150------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f38523d30c0</guid>
            <category><![CDATA[lightgbm]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[fine-tuning]]></category>
            <dc:creator><![CDATA[Selim Hacıömeroğlu]]></dc:creator>
            <pubDate>Sat, 26 Oct 2024 19:13:30 GMT</pubDate>
            <atom:updated>2024-10-27T10:39:58.650Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*lC7w-cbJ26_FEGjnYskahw.jpeg" /><figcaption><a href="https://en.wikipedia.org/wiki/Perceptron#:~:text=The%20first%20hardware%20implementation%20was,demonstrated%20on%2023%20June%201960.">Mark I Perceptron machine</a></figcaption></figure><h3><strong>What is LightGBM?</strong></h3><p>LightGBM(Light Gradient Boosting Machine), is an open-source, distributed, high-performance gradient boosting framework developed by Microsoft. LightGBM simplifies and accelerates the training of large data sets.</p><p>Some frameworks before LightGBM, such as XGBoost and pGBRT, although effective in some cases, were not efficient when the feature dimension was high and the data set was large. The main reason is that these frameworks use a gradient boosting method called Gradient Boosting Decision Tree (GBDT). This method spends a lot of time because it traverses all data samples and examines all possible split points to calculate the information gains. The most important advantage of LightGBM is that it solves this problem and offers two innovative solutions: Gradiend-based One-Side Sampling (GOSS) and Exclusive Feature Bundling (EFB). With these methods, the training process can be accelerated up to 20 times, with the same accuracy.</p><p>Its performance in speed and accuracy has made it a favorite among data scientists and participants in machine learning competitions.</p><h3><strong>Tree Data Structure</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*oigTCT1_vOi7nVlxzZcCWQ.png" /><figcaption>an example of a tree structure</figcaption></figure><p>A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges.</p><p><strong><em>Node</em><br></strong>A node is an entity that contains a key or value and pointers to its child nodes. The last nodes of each path are called leaf nodes or external nodes that do not contain a link/pointer to child nodes. The node having at least a child node is called an internal node.</p><p><strong><em>Edge</em><br></strong>It is the link between any two nodes.</p><p><strong><em>Root</em><br></strong>It is the topmost node of a tree.</p><p><strong><em>Height of a Tree</em><br></strong>The height of a Tree is the height of the root node or the depth of the deepest node.</p><h3><strong>Decision Tree</strong></h3><p>A decision tree is a non-parametric supervised learning algorithm, which is utilized for both classification and regression tasks. It has a hierarchical, tree structure, which consists of a <strong><em>root</em></strong> node, <strong><em>branches</em></strong>, <strong><em>internal</em></strong> nodes and <strong><em>leaf</em></strong> nodes.</p><p>In a decision tree, there’s a starting point called the root node with no incoming branches. From there, branches go to decision nodes, which use features to separate data into groups. These groups are shown by leaf nodes, representing all possible outcomes.</p><p>LightGBM uses decision trees for ranking, classification and regression tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*hocts0Ku98XYMsN-1uBJmg.png" /></figure><blockquote>“Trees are sanctuaries. Whoever knows how to speak to them, whoever knows how to listen to them, can learn the truth.”</blockquote><blockquote>― <em>Herman Hesse</em></blockquote><h3><strong>Leaf-wise and Level-wise Approaches</strong></h3><p>LightGBM uses a leaf-wise growth strategy instead of the traditional level-wise approach.The fundamental difference lies in how the tree grows and how branches expand.</p><p><strong>In Level-wise approach:</strong></p><ul><li>The tree grows <strong><em>level by level</em></strong>. In other words, all nodes are expanded at each level, and the children of these nodes are created.</li><li>This approach typically leads to <strong><em>shallower but wider trees</em></strong><em>.</em></li><li>The tree expansion may stop before ensuring completion of all levels, potentially <strong><em>prolonging the processing time</em></strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ib8cNiTbg4_tVEPcaFUp5w.png" /><figcaption>Level-wise Approach Illustration</figcaption></figure><p><strong>In Leaf-wise approach:</strong></p><ul><li>The tree expands by adding a leaf that offers the highest gain at each step. This means <strong><em>only one leaf node is added </em></strong>with each expansion.</li><li>This usually results in <strong><em>deeper but narrower trees</em></strong>. Deeper trees can offer more flexibility to capture complex feature relationships.</li><li>It is generally <strong><em>faster compared to the level-wise approach.</em></strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KBsM4T4ZmQlwZSyHdHVkqg.png" /><figcaption>Leaf-wise Approach Illustration</figcaption></figure><p>To sum up, LightGBM’s leaf-wise strategy prioritizes expanding the tree by adding leaves with maximum gain, providing benefits like enhanced depth for capturing complex patterns and quicker processing.</p><p>Let’s talk about <em>Gradient Boosting</em>, a very important concept for machine learning, and <em>how LightGBM uses it.</em></p><h3>Gradient Boosting</h3><p>Machine learning models can either be fitted to data one by one or grouped together in what’s called an <em>ensemble</em>. An ensemble is when you combine several basic models to make a stronger, more effective model.</p><p>Machine learning boosting is a technique used to build ensembles. It begins by fitting an initial model, like a tree or linear regression, to the data. Then, another model is created to focus on predicting cases where the first model struggles. By combining these two models, you get a stronger result than using either one alone. This process is repeated multiple times, with each new model trying to improve upon the weaknesses of the combined ensemble of previous models.</p><p>Gradient boosting is a machine learning ensemble technique that combines the predictions of multiple weak learners, typically decision trees, sequentially. It aims to improve overall predictive performance by optimizing the model’s weights based on the errors of previous iterations, gradually reducing prediction errors and enhancing the model’s accuracy.</p><p>LightGBM offers two new boosting methods:</p><ul><li>Gradient-based One-Side Sampling (GOSS)</li><li>Exclusive Feature Bundling (EFB)</li></ul><h3>Gradient-based One-Side Sampling (GOSS)</h3><p>It starts from the fact that different data examples contribute differently to information gain. Examples with <strong>higher gradients have a greater impact on information gain</strong>.</p><p>GOSS preserves examples with large gradients (e.g., those greater than a specific threshold or in the top percentiles), while maintaining the accuracy of information gain predictions by randomly discarding examples with small gradients.</p><p>This method allows for more accurate gain predictions compared to regular random sampling at the desired example ratio when information gain varies widely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qGKE4FKkS62sEjmoz4fEIw.png" /><figcaption>Gradient-Based One-Side Sampling</figcaption></figure><p>Specifically, GOSS firstly sorts the data instances according to the <strong><em>absolute value of their gradients</em></strong> and selects the top a × 100% instances. Then it randomly samples b × 100% instances from the rest of the data.By doing so, model put more focus on the under-trained instances without changing the original data distribution by much.</p><p>Here is the pseudo-code of GOSS:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*H8ucF6jU4ztO3XTDs41TWw.png" /><figcaption>Pseudo-code of GOSS</figcaption></figure><h3>Exclusive Feature Bundling (EFB)</h3><p>It generally provides an almost lossless strategy to represent high-dimensional sparse data with fewer features. Especially in a sparse feature space, many features are <strong>mutually exclusive,</strong> meaning they do not simultaneously take non-zero values.</p><p>These features can be safely merged into a single feature. As a result, the complexity of histogram construction decreases from <strong><em>O(data^feature) to O(data^bundle)</em></strong> levels while <strong><em>bundle &lt;&lt; feature</em></strong>. This speeds up the algorithm while maintaining precision.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPQMZ_n5M5HtLE4fV4HUJQ.png" /><figcaption>Exclusive Feature Bundling</figcaption></figure><p>There are two issues to be addressed: <em>The first one is to determine which features should be bundled together. The second is how to construct the bundle.</em></p><p>Here are the pseudo-codes of these two algorithms:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/1*Im0ov9yQrw0zd3621QFo7w.png" /><figcaption>Pseudo-codes of algorithms related to Exclusive Feature Bundling</figcaption></figure><h3>Parameters of LightGBM</h3><p>LightGBM has more than 100 parameters. Firstly, we will begin specifying the <em>core parameters:</em></p><ul><li><strong><em>objective</em></strong> and <strong><em>metric</em></strong> for your problem setting</li><li><strong><em>seed</em></strong> for reproducibility</li><li><strong><em>num_iterations</em>, <em>learning_rate</em></strong> and <strong><em>early_stopping_round</em> </strong>for training</li></ul><p>Then, let’s discuss most important and commonly used LightGBM hyperparameters:</p><ul><li><strong>Tree Shape</strong> : <em>num_leaves</em> and <em>max_depth</em></li><li><strong>Tree Growth</strong> : <em>min_data_in_leaf</em> and <em>min_gain_to_split</em></li><li><strong>Data Sampling</strong> : <em>bagging_fraction</em>, <em>bagging_freq</em>, and <em>feature_fraction</em></li><li><strong>Regularization</strong>: <em>lambda_l1</em> and <em>lambda_l2</em></li></ul><h4><strong>Tree Shape Parameters</strong></h4><p>As stated in the beginning of the article, LightGBM trees grows <strong>leaf-wise</strong> instead of <strong>level-wise</strong>. We can use <strong><em>num_leaves</em></strong> and <strong><em>max_depth</em></strong> to control the size of a single tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*cYieJwAQ59q5owDN8wWtAQ.png" /></figure><p>The most important parameter to control the tree structure is <strong><em>num_leaves</em></strong><em>, </em>controls the number of decision leaves in a single tree. The decision leaf of a tree is the node where the <em>‘actual decision’</em> happens.</p><p>The next parameter is <strong><em>max_depth</em></strong>. When max_depth is higher, there are more levels in tree, which makes it more complex and prone to overfit. When max_depth is too low, it may be underfit.</p><p>Tuning num_leaves can be easy once you determine max_depth. There is a simple formula given in LGBM documentation — the <strong>maximum limit to num_leaves should be 2^(max_depth)</strong>. This means the optimal value for num_leaves lies within the range (2³, 2¹²) or (8, 4096). <em>However, num_leaves impacts the learning in LGBM more than max_depth.</em> It is therefore preferable to use this parameter in this range: (20, 3000).</p><p>For the parameter <strong>num_leaves</strong>:</p><ul><li>Default: 31</li><li>Tuning range: 8, 4096</li></ul><p>For the parameter <strong>max_depth</strong>:</p><ul><li>Default: -1 (no limit)</li><li>Tuning range: (3, 16)</li></ul><p>The <em>smaller the trees (small num_leaves and max_depth),</em> the <strong>faster the training speed — but this can also decrease accuracy</strong>.</p><h4><strong>Tree Growth Parameters</strong></h4><p>In addition to considering the depth and quantity of leaves, we have option to define the circumstances under which leaf will divide. This allows you to control the growth pattern of the tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*38MwOJ1i9IVkEObkRPXGrQ.png" /></figure><p>The parameter <strong><em>min_data_in_leaf</em></strong><em> </em>defines the minimum number of observations that must meet the decision criteria within a leaf node.</p><p>For instance, if a decision tree leaf in LightGBM evaluates whether a specific feature value is greater than 13, setting the min_data_in_leaf parameter to 100 means that the model will only consider this leaf if at least 100 training samples meet this criterion (i.e., have feature values greater than 13). This threshold helps ensure that the model doesn’t split the data further unless a substantial number of observations support it. The optimal value for min_data_in_leaf depends on the number of training samples and num_leaves.</p><p><em>As stated by LightGBM documentation, setting it to a large value can avoid growing too deep a tree, but may cause under-fitting. For large datasets, set a value in hundreds or thousands.</em></p><p>For the parameter <strong>min_data_in_leaf</strong>:</p><ul><li>Default: 20</li><li>Tuning range: (5,900), this may vary depending on the dataset size. As a general guideline increase it with the size of the dataset.</li></ul><p>For the parameter <strong>min_gain_to_split</strong>:</p><ul><li>Default: 0</li><li>Tuning range: (0,15)</li></ul><p>Restricting tree growth through an increase in the <em>min_gain_to_split</em> parameter can <strong>expedite training time</strong> by generating smaller trees.</p><p>However, this adjustment may come at the cost of <strong>reduced accuracy</strong>, as the resulting trees may be less comprehensive in capturing complex patterns within the data.</p><h4><strong>Data Sampling Parameters</strong></h4><p>Data sampling is a technique to force the model to generalize. The general idea is not to feed the model all the data at each iteration. Instead, the model will only see a <strong>fraction of the training data</strong> at each iteration.</p><p><strong>Bagging</strong></p><p>At every bagging_freq-th iteration, LGBM will randomly select <strong>bagging_fraction * 100 %</strong> of the data to use for the next bagging_freq iterations. E.g., if bagging_fraction = 0.8 and bagging_freq = 2, LGBM will sample 80 % of the training data every second iteration before training each tree.</p><p>This technique <em>can be used to speed up training</em>. For the parameters:</p><ul><li>Default: bagging_fraction = 1.0 and bagging_freq = 0 (disabled)</li><li>Tuning range: bagging_fraction (0.5, 1)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*khV5XjZfQYxO0sbHZwQc3Q.png" /><figcaption>Bagging</figcaption></figure><p><strong>Feature Sampling</strong></p><p>At every iteration, LGBM will randomly select <strong>feature_fraction * 100 %</strong> of the data. E.g., if feature_fraction = 0.8, LGBM will sample 80 % of the features before training each tree.</p><p>For the parameter <strong>feature_fraction</strong>:</p><ul><li>Default: 1</li><li>Tuning range: (0.5, 1)</li></ul><p>Using sub-feature sampling, which is similar to bagging, speeds up training and is helpful when there are <strong>relationships between the features</strong> that can make it harder for the model to learn.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*BslLCCsqV-G5-BMBICufgg.png" /><figcaption>Feature Sampling</figcaption></figure><h4>Regularization Parameters</h4><p>Regularization is a technique used to prevent <strong>overfitting</strong>(training accuracy of the model is high but the validation accuracy or the testing accuracy is low) by adding a penalty term to the loss function, discouraging the model from assigning too much importance to individual features or coefficients. The roles of Regularization:</p><p><strong>1- Complexity Control</strong>: Regularization helps control model complexity by preventing overfitting to training data, resulting in better generalization to new data.</p><p><strong>2- Preventing Overfitting:</strong> One way to prevent overfitting is to use regularization, which penalizes large coefficients and constrains their magnitudes, thereby preventing a model from becoming overly complex and memorizing the training data instead of learning its underlying patterns.</p><p><strong>3- Balancing Bias and Variance:</strong> Regularization can help balance the trade-off between model bias (underfitting) and model variance (overfitting) in machine learning, which leads to improved performance.</p><p><strong>4- Feature Selection:</strong> Some regularization methods, such as L1 regularization (Lasso), promote sparse solutions that drive some feature coefficients to zero. This automatically selects important features while excluding less important ones.</p><p><strong>5- Handling Multicollinearity:</strong> When features are highly correlated (multicollinearity), regularization can stabilize the model by reducing coefficient sensitivity to small data changes.</p><p><strong>6- Generalization:</strong> Regularized models learn underlying patterns of data for better generalization to new data, instead of memorizing specific examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*pNiZ7LVqCF3sw6VD85UXZA.png" /><figcaption><a href="https://www.geeksforgeeks.org/underfitting-and-overfitting-in-machine-learning/">GeeksforGeeks for more details</a></figcaption></figure><p>As the parameter names already suggest, the <strong><em>parameter lambda_l1</em></strong> is used for L1 regularization and <strong><em>lambda_l2</em></strong> for L2 regularization.</p><p>L1 regularization penalizes the absolute values of the weights and thus is <strong>robust against outliers</strong> L2 regularization penalizes the sum of squares of the weights and thus is <strong>sensitive to outliers.</strong></p><p>We can either decide to use only one of the two types of regularization or we can combine them if we want.</p><p>For both parameters, the parameter values behave similarly:</p><ul><li>Default: 0 (disabled)</li><li>Tuning range: (0.01, 100)</li></ul><h4><strong>Parameters for Better Accuracy</strong></h4><p>A common approach to improving accuracy is by using <strong>multiple decision trees and lowering the learning rate</strong>. This means finding the optimal balance between the number of decision trees (n_estimators) and the learning rate in LightGBM.</p><p><strong>n_estimators</strong> controls the number of decision trees while <strong>learning_rate</strong> is the step size parameter of the gradient descent.</p><p>Ensembles like LGBM build trees in iterations, and each new tree is used to correct the “errors” of the previous trees. This approach is fast and powerful, and prone to overfitting.</p><p>That’s why gradient boosted ensembles have a learning_rate parameter that controls the learning speed. Typical values lie within 0.01 and 0.3, but it is possible to go beyond these, especially towards 0. So, the perfect setup for these 2 parameters (n_estimators and learning_rate) is to use many trees with early stopping and set a low value for learning_rate.</p><p>We can also increase max_bin than the default (255) but again, at the risk of overfitting.</p><p>Below you can find an overview of them with their recommended tuning ranges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*y64GJKGiQSD1JbWMffEaOA.png" /><figcaption><a href="https://lightgbm.readthedocs.io/en/latest/Parameters.html#">LightGBM documentation for more details</a></figcaption></figure><p><strong>For Better Accuracy:</strong></p><ul><li>Use large max_bin (may be slower)</li><li>Use small learning_rate with large num_iterations</li><li>Use large num_leaves (may cause over-fitting)</li><li>Use bigger training data</li></ul><p><strong>Deal with Over-fitting</strong>:</p><ul><li>Use small max_bin</li><li>Use small num_leaves</li><li>Use min_data_in_leaf</li><li>Use bagging by set bagging_fraction and bagging_freq</li><li>Use feature sub-sampling by set feature_fraction</li><li>Use bigger training data</li><li>Try lambda_l1, lambda_l2 and min_gain_to_split for regularization</li><li>Try max_depth to avoid growing deep tree</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f38523d30c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>