<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged 0.8</title>
<link>https://coderlegion.com/tag/0.8</link>
<description>Powered by Question2Answer</description>
<item>
<title>From Linear Regression to Gradient Descent</title>
<link>https://coderlegion.com/21640/from-linear-regression-to-gradient-descent</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt; to help more developers discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;In the previous section, we learned that linear regression finds the best-fitting line by determining the optimal slope and intercept.&lt;/p&gt;

&lt;p&gt;In this article, we will discuss how to calculate the optimal slope and intercept using Gradient Descent.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to calculate the optimal slope and intercept using Gradient Descent
&lt;/h2&gt;

&lt;p&gt;The quality of that line is measured using the Sum of Squared Residuals (SSR), which represents the total prediction error.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;SSR = sum( (y_observed - y_predicted)^2 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The best regression line is simply the line that produces the smallest SSR.&lt;/p&gt;

&lt;p&gt;When studying linear regression, it's easy to think that the slope and intercept magically appear from a formula. In reality, they are the values that minimize the prediction error. This is where Gradient Descent comes in.&lt;/p&gt;

&lt;p&gt;Instead of calculating the optimal slope and intercept directly using a closed-form equation, Gradient Descent starts with arbitrary values and gradually improves them. After each step, it measures how the SSR changes and adjusts the parameters in the direction that reduces the error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step Gradient Descent Example
&lt;/h2&gt;

&lt;p&gt;Let's illustrate how Gradient Descent works using the exact same dataset of 4 points from Part 10:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Dataset
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Point 1:&lt;/strong&gt; (1, 2)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Point 2:&lt;/strong&gt; (2, 3)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Point 3:&lt;/strong&gt; (3, 5)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Point 4:&lt;/strong&gt; (4, 4)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Simplifying the Problem
&lt;/h3&gt;

&lt;p&gt;To make the math easy to trace, we will hold the &lt;strong&gt;Slope (m)&lt;/strong&gt; constant at its optimal value of &lt;strong&gt;0.8&lt;/strong&gt; and focus purely on finding the optimal &lt;strong&gt;Intercept (b)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our prediction equation is:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;y_predicted = 0.8 * x + b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We start with an initial guess for the intercept: &lt;code&gt;b = 0&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Calculating the Initial SSR (at b = 0)
&lt;/h3&gt;

&lt;p&gt;Let's find the predicted values and calculate the residuals (&lt;code&gt;observed - predicted&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For Point 1 (1, 2):&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;y_predicted = 0.8 * 1 + 0 = 0.8&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Residual_1 = 2 - 0.8 = 1.2&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;For Point 2 (2, 3):&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;y_predicted = 0.8 * 2 + 0 = 1.6&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Residual_2 = 3 - 1.6 = 1.4&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;For Point 3 (3, 5):&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;y_predicted = 0.8 * 3 + 0 = 2.4&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Residual_3 = 5 - 2.4 = 2.6&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;For Point 4 (4, 4):&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;y_predicted = 0.8 * 4 + 0 = 3.2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Residual_4 = 4 - 3.2 = 0.8&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Now, sum the squared residuals:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;SSR = 1.2^2 + 1.4^2 + 2.6^2 + 0.8^2
    = 1.44 + 1.96 + 6.76 + 0.64
    = 10.8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Derivation of the Gradient (d(SSR)/db)
&lt;/h3&gt;

&lt;p&gt;To know which direction to move the intercept &lt;code&gt;b&lt;/code&gt; and by how much, we take the derivative of SSR with respect to &lt;code&gt;b&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;SSR = sum( (y_observed - (0.8 * x_observed + b))^2 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Applying the chain rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;d(SSR)/db = sum( 2 * (y_observed - (0.8 * x_observed + b)) * (-1) )
          = -2 * sum( y_observed - y_predicted )
          = -2 * sum( Residuals )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gradient is simply &lt;strong&gt;-2 times the sum of the residuals&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Updating the Intercept
&lt;/h3&gt;

&lt;p&gt;The update rule is:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight plaintext&quot;&gt;&lt;code&gt;b_new = b_old - (Learning Rate * Gradient)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's choose a &lt;strong&gt;Learning Rate (LR)&lt;/strong&gt; of &lt;strong&gt;0.1&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gradient:&lt;/strong&gt; &lt;code&gt;d(SSR)/db = -2 * (1.2 + 1.4 + 2.6 + 0.8) = -2 * 6.0 = -12.0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Size:&lt;/strong&gt; &lt;code&gt;Gradient * LR = -12.0 * 0.1 = -1.2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New Intercept:&lt;/strong&gt; &lt;code&gt;b_new = 0 - (-1.2) = 1.2&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With &lt;code&gt;b = 1.2&lt;/code&gt;, the predictions are closer to the actual values.&lt;/li&gt;
&lt;li&gt;The new residuals are: &lt;code&gt;0.0&lt;/code&gt;, &lt;code&gt;0.2&lt;/code&gt;, &lt;code&gt;1.4&lt;/code&gt;, and &lt;code&gt;-0.4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SSR:&lt;/strong&gt; &lt;code&gt;0.0^2 + 0.2^2 + 1.4^2 + (-0.4)^2 = 2.16&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gradient:&lt;/strong&gt; &lt;code&gt;-2 * (0.0 + 0.2 + 1.4 - 0.4) = -2.4&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Size:&lt;/strong&gt; &lt;code&gt;-2.4 * 0.1 = -0.24&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New Intercept:&lt;/strong&gt; &lt;code&gt;b_new = 1.2 - (-0.24) = 1.44&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Step 3 (Convergence):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With &lt;code&gt;b = 1.44&lt;/code&gt;, the new residuals are: &lt;code&gt;-0.24&lt;/code&gt;, &lt;code&gt;-0.04&lt;/code&gt;, &lt;code&gt;1.16&lt;/code&gt;, and &lt;code&gt;-0.64&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gradient:&lt;/strong&gt; &lt;code&gt;-2 * (-0.24 - 0.04 + 1.16 - 0.64) = -0.48&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Size:&lt;/strong&gt; &lt;code&gt;-0.48 * 0.1 = -0.048&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New Intercept:&lt;/strong&gt; &lt;code&gt;b_new = 1.44 - (-0.048) = 1.488&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We repeat this loop. As we approach the optimal intercept, the residuals sum up closer to 0, which shrinks the gradient and steps.&lt;/li&gt;
&lt;li&gt;After several iterations, the gradient becomes 0, and the intercept converges to the exact optimal value of &lt;strong&gt;1.5&lt;/strong&gt; (where SSR reaches its minimum value of &lt;strong&gt;1.8&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We started with an arbitrary intercept of &lt;strong&gt;0&lt;/strong&gt; and adjusted it step-by-step. Each step was guided by the gradient, which told us exactly how much to change the intercept to reduce the prediction error (SSR). We repeated this process until the error reached its minimum.&lt;/p&gt;

&lt;p&gt;While this example focused on a simple linear regression with a single variable, this same principle applies to deep neural networks with millions of parameters. Gradient descent is the engine that drives learning in machine learning.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src=&quot;https://www.youtube.com/embed/4JW4rfheX3U&quot;&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnuphqwcp6bha9ol6pdo3.png&quot; alt=&quot;git-lrc&quot; width=&quot;360&quot; height=&quot;540&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any feedback or contributors are welcome! It&amp;rsquo;s online, source-available, and ready for anyone to use.&lt;/p&gt;

&lt;p&gt;&amp;#11088; &lt;a href=&quot;https://github.com/HexmosTech/git-lrc?utm_source=chatgpt.com&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star git-lrc on GitHub&lt;/a&gt;&lt;/p&gt;
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/21640/from-linear-regression-to-gradient-descent</guid>
<pubDate>Tue, 30 Jun 2026 20:23:06 +0000</pubDate>
</item>
</channel>
</rss>