<?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 Pelin Azizoglu Tonya on Medium]]></title>
        <description><![CDATA[Stories by Pelin Azizoglu Tonya on Medium]]></description>
        <link>https://medium.com/@azizoglupelin?source=rss-d185eb5cba76------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*l2zN60yW0caPskkIbj_BLw.jpeg</url>
            <title>Stories by Pelin Azizoglu Tonya on Medium</title>
            <link>https://medium.com/@azizoglupelin?source=rss-d185eb5cba76------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 14:43:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@azizoglupelin/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[Gradient Descent Algorithm]]></title>
            <link>https://azizoglupelin.medium.com/gradient-descent-algorithm-bf64ed1fde65?source=rss-d185eb5cba76------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf64ed1fde65</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[gradient-descent]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Pelin Azizoglu Tonya]]></dc:creator>
            <pubDate>Thu, 18 Mar 2021 11:22:40 GMT</pubDate>
            <atom:updated>2021-11-13T14:25:53.217Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tJitaO2msdzk6XAy7mtY1Q.jpeg" /><figcaption>Photo by Mukaddes Azizoğlu</figcaption></figure><p>Gradient descent is generally attributed to Cauchy, who first suggested it in 1847. <strong>Gradient descent</strong> is a first-order iterative optimization algorithm for finding a local minimum of a differentiable function. The idea is to take repeated steps in the opposite direction of the gradient (or approximate gradient) of the function at the current point, because this is the direction of steepest descent [1].</p><p><strong>WHY THE GRADIENT DESCENT ALGORITHM NECESSARY?</strong></p><p>One of the primary purposes of neural networks is to calculate the weights. We expect the network that we have created to make choices as close as to correct values. Using the error metric to find out how wrong the predictions are. And a common metric is the sum of the squared error (SSE).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*vHrKrqlfvRRHjUvKPAZSdg.png" /><figcaption>Sum of squared error (SSE)</figcaption></figure><p><em>y’</em> represents the prediction, and y is the correct value.</p><p><em>j </em>represents the output units of the network and <em>μ </em>represents the data points of the network.</p><p><strong>Firstly,</strong> the inside sum over j means for each output unit, find the difference between the correct value <em>y </em>and the predicted value from the network <em>y’</em>, then square the difference, then sum up all those squares.</p><p><strong>Then, </strong>the other sum over <em>μ </em>is a sum over all data points. It means for each data point that calculated the inner sum of the squared differences for each output unit. Then, sum up those squared differences for each data point. That gives the overall error for all the output predictions for all the data points.</p><p>The square ensures the error is always positive, and larger errors are penalized more than smaller errors. And it makes SSE a good choice.</p><p>If we turn back to the inside of the bracket, the prediction value depends on the weights. Accordingly, it affects the SSE formula.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*h9qctR0L9Ps9TDCtm-0EEQ.png" /></figure><p>Purpose of this process the network’s prediction error to be as small as possible. And to make it happen, our goal is to find weights <em>wij </em>that minimizes the squared error <em>E</em>. And here, to do this with a neural network, <strong>gradient descent </strong>is necessary.</p><p><strong>WHAT IS THE GRADIENT DESCENT ALGORITHM AND HOW IT WORKS?</strong></p><p>Multiple small steps should be taken towards the goal that changes the weights in steps that reduce the error. The steps taken should be in the direction that minimizes the error the most. This direction can found by calculating the <em>gradient</em> of the squared error.</p><p>The gradient is a derivative generalized to functions with more than one variable. Calculus can be used to find the gradient at any point in our error function, which depends on the input weights.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*i5osFv_M6KQhugMfzv1DMA.png" /></figure><p>I have plotted an example that where points on a line have the same error. At each step, you calculate the error and the gradient. Then use those to determine how much to change each weight. Repeating this process will eventually find weights that are close to the minimum of the error function, the red dot in the middle.</p><p><strong>Note:</strong> If the weights are initialized with the wrong values, gradient descent could lead the weights into a <strong>local</strong> minimum, it can end up where the error is low, but not the lowest, plotted below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*yl4V8EOIKVg6FrOaJkGQug.png" /></figure><p><strong>An example:</strong></p><p>Lets find the local minima of the function y=(x-3)² with the starting point x=12.</p><p><strong>Step 1:</strong> Initialize x =12. Then, find the gradient of the function, δy/δx= 2*(x-3).</p><p><strong>Step 2: </strong>Move in the negative of the gradient direction with a learning rate. Assume that, learning rate is 0.02.</p><p>x₁ = x₀-(learning rate*δy/δx) → x₁= 12-(0.02*2(12-3)) = 11.64</p><p>x₂ = x₁-(learning rate*δy/δx) → x₂ = 11.64-(0.02*2(11.64-3)) = 11.2944</p><p>Small steps are taken in the right direction. And we are keeping to do this math until reaching the local minima, 3.</p><p><strong>Let’s look at the codes of this process:</strong></p><pre>current_x = 12<br>learning_rate = 0.02<br>previous_step_size = 1 <br>max_iteration = 1000<br>precision = 0.000001<br>iteration = 0<br>x_values = []<br>y_values = []<br>def function(x): # Gradient of function<br>    return 2*(x-3)</pre><pre>while previous_step_size &gt; precision and iteration &lt; max_iteration:<br>    previous_x = current_x<br>    current_x = current_x - (learning_rate * function(previous_x)) # Gradient descent<br>    x_values.append(previous_x)<br>    y_values.append((previous_x-3)**2)<br>    previous_step_size = abs(current_x - previous_x)<br>    iteration += 1<br>    print(&quot;Iteration:&quot;, iteration, &quot;X:&quot;, current_x, &quot;\n&quot;)<br>    <br>print(&quot;The local minimum:&quot;, current_x)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*b6j0N9oTVjdrtlZLuLeeoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*YysOedjzZdUQIzSj4V9dog.png" /></figure><p>As wee see, the algorithm runs for 315 iterations before it terminates.</p><pre>a=[]<br>b=[]<br>for x in range(0,15,1):<br>    y=(x-3)**2<br>    a.append(x)<br>    b.append(y)</pre><pre>fig = plt.figure()<br>axes=fig.add_subplot(111)<br>axes.plot(a,b, &#39;k&#39;)<br>plt.plot(x_values, y_values, &quot;-o&quot;)<br>x0 = x_values[-1]<br>y0 = y_values[-1]<br>plt.plot(x0, y0, &quot;s&quot;)</pre><pre>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*a6vIgDw5KSddpxcKyNwgJQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m7tLA8NFKTBQNmH0mGs3DA.jpeg" /><figcaption>Photo by Mukaddes Azizoğlu</figcaption></figure><p><strong>Mathematics used in Gradient Descent in Neural Networks</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qh4VVn36S9PmvP3ZsRJeuQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*vHrKrqlfvRRHjUvKPAZSdg.png" /></figure><p>First, we need some measures of how bad our predictions are. The obvious choice is to use the difference between the true target value y, and the output, y’. However, if the prediction is too high, this error will be negative, and if their prediction is too low by the same amount the error will be positive. We’d rather treat these errors the same to make both cases positive we’ll just square the error. Squaring error instead of taking absolute is better because it penalizes outliers more than small errors.</p><p>This is the error for just one prediction though.</p><p><em>E = (y -y’)²</em></p><p>And this is the error for the entire dataset. Also, we add one half in front because it cleans up the math for derivation. This formulation is typically called the sum of the squared errors (SSE).</p><p><em>E = </em>½ <em>∑ (yμ -y’μ)²</em></p><p>y’ was the linear combination of the weights and inputs passed through that activation function. As we see, the error depends on the weights, wᵢ, and the input values, xᵢ.</p><p><em>E = </em>½<em> ∑ (yμ-f( ∑ w</em>ᵢ<em>x</em>ᵢ<em>μ))²</em></p><p>Here is a simple depiction of the error with one weight. Our goal is to find the weight at the bottom of this bowl. Starting at some random weight, we want to make a step in the direction towards the minimum. This direction is the opposite of the gradient, the slope. If we take many steps, always descending down a gradient. Eventually, the weight will find the minimum of the error function and this is <strong>gradient descent.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*QdpgGFFOQrIZLo77Kq1jkQ.png" /></figure><p>We want to update the weight, so a new weight, wᵢ, is the old weight, wᵢ plus this weight step, delta wᵢ. This Greek letter, delta, typically signifies change.</p><p>wᵢ = wᵢ + Δwᵢ</p><p>The weight step is proportional to the gradient, the partial derivative of the error with respect to each weight, wᵢ.</p><p>Δwᵢ ∝ -δE / δwᵢ → the gradient</p><p>We can add an arbitrary scaling parameter that allows us to set the size of the gradient descent steps. This is called the learning rate, signified by the Greek letter eta (η).</p><p>Δwᵢ = -η (δE / δwᵢ)</p><p>Calculating the gradient here requires multivariable calculus, partial derivatives.</p><p>δE / δwᵢ = δ/ δwᵢ(½ (y-y’)² )</p><p>= δ/ δwᵢ(½ (y -y’(wᵢ))² )</p><p>Writing out the gradient, you get the partial derivative with respect to the weights of the squared error. The network output, y’, is a function of the weights. And this requires using the chain rule to calculate the derivative.</p><p><strong>CHAIN RULE REFRESHER</strong></p><p><em>δ / δz p(q(z)) = δp/ δq * δq/ δz</em></p><p>q = (y-y’(wᵢ))</p><p>p = ½q(wᵢ)₂</p><p>And then we are taking the derivative with respect to wᵢ. The derivative op p with respect to q returns the error itself. The 2 in the exponent drops down and cancels out the 1/2. Then we are left with the derivative of the error with respect to wᵢ.</p><p>δE / δwᵢ = (y-y’) δ/ δwᵢ(y-y’)</p><p>The target value y does not depend on the weights, but y’ does. Using the chain rule again, the minus sign comes out in front and we are left with the partial derivative of y’. If you remember, y’ is equal to the activation function at h. Where h is the linear combination of the weights and input values.</p><p>y’ = f(h) where h = <em>∑ w</em>ᵢ<em>x</em>ᵢ</p><p>Taking the derivative of y’, and again using the <strong>Chain Rule</strong>. You get the derivative of the activation function at h, times the partial derivative of the linear combination.</p><p>δE / δwᵢ =-(y-y’) δy’ / δwᵢ</p><p>= -(y-y’) f’(h) δ / δwᵢ <em>∑ w</em>ᵢ<em>x</em>ᵢ</p><p>In sum, there is only one term that depends on each weight. Writing this out for weight one, you see that only the first term with x₁ depends on weight one.</p><p>δ / δwᵢ <em>∑ w</em>ᵢ<em>x</em>ᵢ<em> = </em>δ / δw₁ [<em> w</em>₁x₁<em> </em>+ <em>w</em>₂<em>x</em>₂<em> </em>+ <em>…</em>+ <em>wnxn </em>+]</p><p><em>= x</em>₁<em> + 0 + 0 + 0 + …</em></p><p>Then the partial derivative of the sum with respect to weight one is just <em>x</em>₁. All the other terms are 0, then the partial derivative of this sum with respect to wᵢ is just xᵢ.</p><p>Finally putting all this together, the gradient of the squared error with respect to wi is the negative of the error times the derivative of the activation function at h times the input value xᵢ.</p><p>δE / δwᵢ = -(y-y’) f’(h) xᵢ</p><p>Then the weights step is a learning rate eta times the error, times the activation derivative, times the input value.</p><p>Δwᵢ = -η (y-y’) f’(h) xᵢ</p><p>For convenience, and to make things easy, we can define an error term, delta, as the error times the activation function derivative at h.</p><p>Then we can write our weight update as wᵢ equals wᵢ plus the learning rate times the error term times xᵢ is the value of input i.</p><p><em>δ = (y-y’) f’(h)</em></p><p>wᵢ = wᵢ + η <em>δ </em>xᵢ</p><p>You might be working with multiple output units. You can think of this as just stacking the architecture from the single output network but connecting the input units to the new output units. Now the total error would include the error of each output sum together.</p><p>The gradient descent step can be extended to a network with multiple outputs by calculating an error term for each output unit denoted with subscript j.</p><p>If multiple output units:</p><p>δⱼ = (yᵢ -y’ᵢ) f’(hⱼ)</p><p>Δwᵢⱼ = ηδⱼ xᵢ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xZad9j6gwWx2YS1idlHJIg.png" /></figure><p>I hope this article helps you! ☺</p><h3>References</h3><p>[1] <a href="https://en.wikipedia.org/wiki/Gradient_descent">https://en.wikipedia.org/wiki/Gradient_descent</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf64ed1fde65" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gradient Boosting Regression]]></title>
            <link>https://azizoglupelin.medium.com/gradient-boosting-regression-8be8f6dbe2ef?source=rss-d185eb5cba76------2</link>
            <guid isPermaLink="false">https://medium.com/p/8be8f6dbe2ef</guid>
            <category><![CDATA[regression]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[gradient-boosting]]></category>
            <dc:creator><![CDATA[Pelin Azizoglu Tonya]]></dc:creator>
            <pubDate>Sun, 02 Aug 2020 14:44:08 GMT</pubDate>
            <atom:updated>2022-07-07T18:55:56.810Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O-O6g0N3bE3X5fqOKdQgyQ.jpeg" /><figcaption>Photo by Mukaddes Azizoğlu</figcaption></figure><p>Self-driving cars, recommendation systems, or recognition systems… There is no doubt, but in today’s world, we are at the point where science fiction becomes our reality day by day. Well, how did we come to that point? How are machines programmed to mimic human actions, to learn by themselves, or to solve problems? How do we get computers to act without being explicitly programmed, that is to say, how machine learning is achieved?</p><p>In the field of Computer Science (CS), we see that machine learning is one of the hottest topics in AI, in which computer algorithms are used so that the machines can autonomously learn from data and information; and change and improve their algorithms by themselves. There are lots of algorithms and techniques in machine learning, and, as a CS student, I wanted to explain one specific technique and its mathematical fundamentals, which I believe is essential to understand the algorithms, namely Gradient Boosting Technique.</p><p><strong>What is the Gradient Boosting Technique?</strong></p><p>Gradient Boosting is a machine learning technique for regression and classification problems. It produces a prediction model in the form of an ensemble of weak prediction models, which are typically decision trees [1]. Very briefly, the perspective of this technique is that in a testing dataset taking lots of small steps in the right direction provides better predictions for the dataset [2]. Well, what does this mean? Let me explain it with an example. You will see that we’ll be basically doing the same thing a few times to understand the rationale of the technique.</p><p>So, let’s start!</p><p><strong>How Gradient Boosting works?</strong></p><p>Here is a short table that I created by using a part of a dataset imported from Kaggle [3]. We have 6 people and information related to their chest pain, exercise-induced angina, and maximum heart rate achieved. I want to determine whether someone has heart disease with the Gradient Boosting technique.</p><p><strong>Chest Pain, </strong>0: Minimum, 3: Maximum</p><p><strong>Exercise Induced Angina, </strong>0: No, 1: Yes</p><p><strong>Heart Disease,</strong> 0: No, 1: Yes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iQdAUEJ7FMxQWn8O0owWBA.png" /><figcaption>Table 1</figcaption></figure><p>Here are the steps of how we will use the Gradient Boosting technique to get its perspective:</p><ul><li>The first thing to do is to calculate the average of the presence/absence of heart disease. This will be our first prediction for everyone’s heart disease in our sample, (0+1+0+1+0+1) / 6 = <strong>0.5</strong>.</li><li>The next thing to do is find the difference between observed heart disease in people in our sample and our first prediction (0.5). We will name this difference as the <strong>residual</strong> value.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8aPQanWBSjAfubHi7FfNkQ.png" /><figcaption>Table 2</figcaption></figure><ul><li>Now, we will build a decision tree(for more information about decision trees, you can search). In this tree, we will consider all the variables, chest pain, exercise-induced angina, and maximum heart rate achieved, given in Table 2. In the end, we will add the residuals in it, so roughly, a tree based on residuals will be created.</li><li>At this point, we have an essential issue: Here, the number of leaves of the tree should be less than the numbers of data given in the table. In this example, I used six different information, which allows up to four leaves. Of course, when using a larger dataset, the number of leaves can be higher. As a result of used six data and four leaves, two rows of data go to the same leaf. Therefore I replaced these residuals with their average. So, here is our tree:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*sNMbmMRwoCyTGVrxCooqGA.png" /></figure><ul><li>Now, it is time to use the previous prediction and leaf values to make a new prediction:</li></ul><p>(New) Prediction = (First Prediction) + (<strong>Learning Rate</strong> * Residual)</p><p>For example, the calculation of the prediction value is as follows for the first row of the table:</p><p>(New) Prediction = 0.5 + (0.1 * -0.5) = 0.5 -0.05 = 0.45</p><p><strong>Note:</strong> The 0.1 value here is the learning ratio, which takes a value between 0 and 1.</p><ul><li>At this step, we will calculate new residuals just as we did before. However, here we will subtract the predicted value at the last stage from the observed heart disease value. The calculation of the residual value is as follows for the first row of the table:</li></ul><p>(New) Residual = 0 -(0.5 + (0.1 * -0.5)) = -0.45</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MyyQEH98YGFNCfXg_bIdkw.png" /><figcaption>Table 3</figcaption></figure><ul><li>The next thing to do is, again, build a tree based on the new residuals just as we did before.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nK-nCsIRha1HFAofhNP8OA.png" /></figure><ul><li>Not finished yet. Let’s go one step further:</li><li>Again, we will make a new prediction by using previous prediction (0.5 + (0.1 * -0.5)) and leaf values (-0.45).</li></ul><p>New Prediction = Previous prediction + (learning rate * leaf values)</p><p>The calculation of the prediction value is as follows for the first row of the table:</p><p>New Prediction = 0.5 + (0.1 * -0.5) + (0.1 * -0.45) = 0.405</p><p>Continuing…</p><ul><li>We will calculate new residuals by subtracting the predicted value at the last stage (0.405) from the observed heart disease value.</li></ul><p>The calculation of the residual value is as follows for the first row of the table:</p><p>Residual = 0 -(0.5 + (0.1 * -0.5) + (0.1 * -0.45)) = -0.405</p><p>So, the point we reach here is that, if we compare the first residual value and the latest residual value, it can be clearly seen that the new remaining values are approaching 0, that is, small steps are taken in the right direction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qH34CHaO4uISCnkx5-xLdA.png" /><figcaption>Table 4</figcaption></figure><ul><li>What we need to do in Gradient Boosting is that keeping to make tree until reaching the adding an additional tree does not make residuals closer to zero anymore, or maximum.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R6cd5n53Nx2NR4BjOe2xTA.png" /></figure><p><strong>Now, let’s go through the same data (Table 1) to understand the mathematics used in Gradient Boosting Regression.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JqYtvZFqX4lKQyq21A-atg.png" /></figure><p>Now, let’s go through these steps one by one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Rr0xuqIGimDtdYSMRklhQ.png" /></figure><p><strong>Input</strong></p><ul><li><strong><em>xᵢ</em> </strong>represents properties (chest pain, exercise-induced angina, maximum heart rate achieved) to be used to predict heart disease in each person.</li><li><strong><em>yᵢ</em> </strong>represents heart disease.</li><li>The value of <strong><em>i</em> </strong>changes according to the person. It is the place of the person in the table.</li><li>The value of <strong><em>n</em> </strong>indicates the total number of people. For this example, <strong><em>n</em> = 6</strong>.</li></ul><p><strong>Differentiable Loss Function</strong></p><ul><li><em>L(</em><strong><em>yᵢ</em></strong><em>, F(X))</em> is usually taken as ½ (Observed -Prediction)².</li></ul><p>Here we have ½ in the function since multiplying by a fixed number of ½ does not affect the result much. So, we can say that the function is generally multiplied by ½ for ease of derivation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V7J2zSJ2M-tpE2PzwlSF3w.png" /></figure><p><strong>Step 1</strong></p><ul><li>The part within the summation sign sigma is the Loss Function.</li><li><strong><em>yᵢ</em> </strong>refers to the observed values (Table 1; heart disease values)</li><li><strong><em>Ɣ</em> </strong>refers to the predicted values.</li><li>So,<strong> </strong>the summation means adding up each observed value for the Loss Function.</li><li>The argmin<em>Ɣ </em>means find a predicted value that minimizes this sum. To do this, the derivative is taken and then, it is equalized to 00</li><li>Here is the process:</li></ul><p><em>f₀(x) </em>= <strong>d / dPrediction</strong> (½ (0 -Prediction)² + ½ (1 -Prediction)² + ½ (0-Prediction)² + ½ (1-Prediction)² + ½ (0 -Prediction)² + ½ (1 -Prediction)²)</p><p>= -(0-Prediction )-(1-Prediction )-(0-Prediction )-(1-Prediction )-(0-Prediction )-(1-Prediction ) = 0</p><p>=&gt;<em> f₀(x) </em>= <strong>Prediction</strong> = (0 + 1 + 0 + 1 + 0 + 1) / 6 = 0.5</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*INB22WiwZ1NY33Jn26oGVA.png" /></figure><p><strong>Step 2</strong></p><ul><li><strong>for m = 1 to M</strong> part, <strong>m </strong>refers to an individual tree, <strong>M </strong>refers to the total count of trees since this is the first tree, m = 1.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GXJXjYCrJSqP_kwKHiozag.png" /></figure><ul><li><strong>a)</strong> Inside of square brackets is derivation of the Loss Function with respect to the predicted value.</li></ul><p>Minus sign means to multiply the derivative by -1. This means that we subtract the predicted value from the observed value. This gives us the residual value.</p><p>Let’s apply this:</p><p>Since m = 1, <em>fm-</em>₁(x) equals to <em>f</em>₀(x) = 0.5.</p><p><em>r</em><strong><em>ᵢ</em></strong><em>,₁</em> = (Observed -0.5)</p><p><em>r₁,₁</em> = (0-0.5) = -0.5</p><p><em>r₂,₁</em> = (1-0.5) = 0.5</p><p><em>r₃,₁ </em>= (0-0.5) = -0.5</p><p><em>r₄,₁</em> = (1-0.5) = 0.5</p><p><em>r₅,₁ </em>= (0-0.5) = -0.5</p><p><em>r₆,₁</em> = (1-0.5) = 0.5</p><p>(Table 2)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nOE6SqAKMLpZfvPcEt5CQA.png" /></figure><ul><li><strong>b) </strong>Now we create a tree with the values <em>r</em><strong><em>ᵢ</em></strong><em>,₁</em>.</li></ul><p>j refers to the number of leaves (i.e., j:1, j:2, j:3, j:4 for our example).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Eo6R5KOCnB5QZNTEd5EIKA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7OJwFhMIOEv-3--oFV8_Pg.png" /></figure><ul><li><strong>c)</strong> In this part, we will determine the output values for each leaf.</li></ul><p>Since it is our first leaf j = 1 and first tree m = 1 at this point. Realize that we have the Loss Function within the summation sign.</p><p>Now, let’s look at the calculations:</p><ul><li><strong>For <em>R</em></strong><em>₁,₁</em><strong>,</strong></li></ul><p><em>Ɣ₁,₁</em> = argmin<em>Ɣ</em> [½(<em>y₁</em> -(<em>Fm-</em>₁<em>(x₁) </em>+ <em>Ɣ</em>))² + ½(<em>y₅</em> -(<em>Fm-</em>₁<em>(x₅)</em> + Ɣ))²]</p><p><em>Ɣ₁,₁</em> = argmin<em>Ɣ</em> [½(0 -(<em>F₀(x₁) </em>+ <em>Ɣ</em>))² + ½(0 -(<em>F₀(x₅)</em> + <em>Ɣ</em>))²]</p><p><em>Ɣ₁,₁</em> = argmin<em>Ɣ</em> [½(0-(0.5 + <em>Ɣ</em>))² + ½(0 -(0.5 + <em>Ɣ</em>))²]</p><p><em>Ɣ₁,₁</em> = argmin<em>Ɣ</em> [½(-0.5 -<em>Ɣ</em>)² + ½(-0.5 -<em>Ɣ</em>)²]</p><p><em>Ɣ₁,₁</em> = d/d<em>Ɣ</em> [½(-0.5-<em>Ɣ</em>)² + ½(-0.5-<em>Ɣ</em>)²]</p><p>0.5<em> </em>+ <em>Ɣ</em> + 0.5 + <em>Ɣ</em> = 0</p><p><em>Ɣ</em> = (-0.5–0.5) / 2</p><p>=&gt; <em>Ɣ</em>₁,₁ = -0.5</p><ul><li><strong>For <em>R</em></strong><em>₂,₁</em><strong>,</strong></li></ul><p>(We only have x₄ here, therefore, we do not need the summation sign.)</p><p><em>Ɣ₂,₁</em> = argmin<em>Ɣ</em> ½(<em>y₄</em> -(<em>Fm-</em>₁<em>(x₄)</em> + <em>Ɣ</em>))²</p><p><em>Ɣ₂,₁</em> = argmin<em>Ɣ </em>½(<em>y₄</em>-<em>(F₀(x₄)</em> + Ɣ))²</p><p><em>Ɣ₂,₁</em> = argmin<em>Ɣ</em> ½(1 -(0.5 + Ɣ))²</p><p><em>Ɣ₂,₁ </em>= <em>argminƔ </em>½(0.5 -Ɣ))²</p><p><em>Ɣ₂,₁ </em>= d/d<em>Ɣ</em> ½(0.5-Ɣ))²</p><p>-0.5<em> </em>+ <em>Ɣ</em> = 0</p><p>=&gt; <em>Ɣ</em>₂,₁ = 0.5</p><ul><li><strong>For <em>R</em></strong><em>₃,₁</em>,</li></ul><p>(We only have x₃ here, therefore, we do not need the summation sign.)</p><p><em>Ɣ₃,₁</em> = argmin<em>Ɣ</em> ½(<em>y₃</em>-(<em>Fm-₁(x₃)</em> +<em> Ɣ</em>))²</p><p><em>Ɣ₃,₁</em> = argmin<em>Ɣ</em> ½(<em>y₃</em> -(<em>F₀(x₃)</em> +<em> Ɣ</em>))²</p><p><em>Ɣ₃,₁ </em>= argmin<em>Ɣ</em> ½(0-(0.5+ <em>Ɣ</em>))²</p><p><em>Ɣ₃,₁</em> = <em>argminƔ </em>½(-0.5 -<em>Ɣ</em>))²</p><p><em>Ɣ₃,₁</em> = d/d<em>Ɣ</em> ½(-0.5 -<em>Ɣ</em>))²</p><p>0.5<em> +</em> <em>Ɣ </em>= 0</p><p>=&gt; <em>Ɣ</em>₃,₁ = -0.5</p><ul><li><strong>For <em>R</em></strong><em>₄,₁</em>,</li></ul><p><em>Ɣ₄,₁</em> = argmin<em>Ɣ</em> [½(<em>y₂</em> -(<em>Fm-₁(x₂)</em> + <em>Ɣ</em>))² + ½(<em>y₆</em> -<em>(Fm-₁(x₆)</em> + <em>Ɣ</em>))²]</p><p><em>Ɣ₄,₁</em> = argmin<em>Ɣ</em> [½(1 -(<em>F₀(x₂)</em> + <em>Ɣ</em>))² + ½(1-(<em>F₀(x₆) </em>+ <em>Ɣ</em>))²]</p><p><em>Ɣ₄,₁</em> = argmin<em>Ɣ</em> [½(1-(0.5 + <em>Ɣ</em>))² + ½(1 -(0.5 + <em>Ɣ</em>))²]</p><p><em>Ɣ₄,₁</em> = argmin<em>Ɣ </em>[½(0.5-<em>Ɣ</em>)² + ½(0.5 -<em>Ɣ</em>)²]</p><p><em>Ɣ₄,₁</em> = d/d<em>Ɣ</em> [½(0.5-<em>Ɣ</em>)² + ½(0.5 -<em>Ɣ</em>)²]</p><p>-0.5<em> </em>+ <em>Ɣ</em> -0.5 +<em> Ɣ </em>= 0</p><p><em>Ɣ</em> = (0.5 + 0.5) /2</p><p>=&gt; <em>Ɣ</em>₄,₁ = 0.5</p><p>As we see, the output values are equal to the mean of the residuals within the same leaves.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*8pZdPOAF-cv7Tpg1tHoB_A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LIPHYj0yFWOgKqdV6qV5ug.png" /></figure><ul><li><strong>d)</strong> Now, we will make new predictions.</li></ul><p>Since it is the first tree, m = 1, and the new prediction is <em>f₁(x)</em>.</p><p>The formula is as <em>f₁(x)</em> = <em>f₀(x)</em> + (0.1 * the output value in the tree).</p><ul><li>Since chest pain &lt;= 0, output = -0.5 in the first row</li></ul><p><em>f₁(x₁) </em>= 0.5<em> </em>+ (0.1 * -0.5) = 0.45, output is closer to heart disease(0) than the first guess.</p><ul><li>Since chest pain &gt; 0 and maximum heart rate achieved &gt; 152, output = 0.55 in the second row</li></ul><p><em>f₁(x₂)</em> = 0.5 + (0.1 * 0.5) = 0.55, output is closer to heart disease(1) than the first guess.</p><ul><li>Since chest pain &gt; 0, maximum heart rate achieved &lt;= 152 and exercise induced angina = 0, output =0.45 in the third row</li></ul><p><em>f₁(x₃)</em> = 0.5<em> </em>+ (0.1 * -0.5) = 0.45, output is closer to heart disease(0) than the first guess.</p><ul><li>Since chest pain &gt; 0, maximum heart rate achieved &lt;= 152 and exercise induced angina = 1, output = 0.55 in the fourth row</li></ul><p><em>f₁(x₄) </em>= 0.5<em> </em>+ (0.1 * 0.5) = 0.55, output is closer to heart disease(1) than the first guess.</p><ul><li>Since chest pain &lt;= 0, output = 0.45 in the fifth row</li></ul><p><em>f₁(x₅)</em> = 0.5<em> </em>+ (0.1 * -0.5) = 0.45, output is closer to heart disease(0) than the first guess.</p><ul><li>Since chest pain &gt; 0 and maximum heart rate achieved &gt; 152, output = 0.55 in the sixth row</li></ul><p><em>f₁(x₆)</em> = 0.5<em> </em>+ (0.1 * 0.5) = 0.55, output is closer to heart disease(1) than the first guess.</p><p>The <strong>first</strong> iteration of <strong>Step 2 </strong>has finished. The next step is to make m = 2 and repeat the same steps we did in the Step 2. We have to do this before going through with the Step 3 until m is equal to M.</p><p>Here, I’ll continue with the Step 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J4zns-0R8SVqDfbiuOsEyQ.png" /></figure><p><strong>Step 3</strong></p><ul><li>Up until this point, we repeated the step 2 until m is equal to M. Now, in the Step 3, we have a <strong><em>fM(x) </em></strong>function. With this function, it is possible to estimate whether someone had heart disease.</li></ul><p><strong>Let’s look at the codes of this process:</strong></p><pre>import numpy as np<br>import pandas as pd<br>from sklearn import ensemble<br>from matplotlib import pyplot as plt</pre><p>All the necessary Python libraries were imported.</p><pre>data = pd.read_csv(“dataset.xls”, sep = “,”)<br>data.describe()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0gtUzSOft8fL-0pZSdvEjg.png" /></figure><pre>X_features = data.iloc[:,:-1]<br>y_target = data.iloc[:,-1]</pre><p>Data was read. I determined the target and features.</p><pre>plt.figure(figsize=(12,1))<br>plt.title(“Gradient Boosting Regression”, color = ‘purple’)<br>plt.scatter(X_features[‘age’],y_target, color = ‘pink’)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wV77jVgyjo0Cjb9aGePB-w.png" /></figure><p>Output and one feature can also be checked. For example, here, I checked the distribution of the presence of heart disease in different ages.</p><pre>person = [[65,1,3,145,233,1,0,150,0,2.3,0,0,1]]<br>prediction = model.predict(person)<br>print(prediction[0])<br>print(‘Prediction: %d’ % round(prediction[0]))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dBn4inFiDsBnfrx136pg6Q.png" /></figure><p>Here, I created a random data, a person who has different features to predict if s/he has heart disease.</p><pre>model = ensemble.GradientBoostingRegressor(learning_rate = 0.125)<br>model.fit(X_features,y_target)<br>#learning_rate = 0.125, total_tree_count = 100, 303 data<br>model.score(X_features, y_target)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QaSao5dkkXgVK7q7pGw2AA.png" /></figure><p>Accuracy was checked at 0.125 learning rate.</p><p>So, that’s the point! As students, we may want end-run but, I genuinely believe we should learn the rationale behind the algorithms we use. I hope this article helps you all! ☺</p><h3>References</h3><p>[1] <a href="https://en.wikipedia.org/wiki/Gradient_boosting">https://en.wikipedia.org/wiki/Gradient_boosting</a></p><p>[2] <a href="https://statweb.stanford.edu/~jhf/ftp/trebst.pdf">https://statweb.stanford.edu/~jhf/ftp/trebst.pdf</a></p><p>[3] <a href="https://www.kaggle.com/ronitf/heart-disease-uci">https://www.kaggle.com/ronitf/heart-disease-uci</a></p><p>[4] <a href="https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingRegressor.html">https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingRegressor.html</a></p><p>[5]<a href="https://www.stat.auckland.ac.nz/~yee/784/files/ch10BoostingAdditiveTrees.pdf">https://www.stat.auckland.ac.nz/~yee/784/files/ch10BoostingAdditiveTrees.pdf</a></p><p><em>Thanks to Göksu Akşam for helping me editing.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8be8f6dbe2ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>