<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linearregression on davethehuman.com</title><link>https://davethehuman.com/tags/linearregression/</link><description>Recent content in Linearregression on davethehuman.com</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>© 2026 Dave the human</copyright><lastBuildDate>Tue, 06 Oct 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://davethehuman.com/tags/linearregression/index.xml" rel="self" type="application/rss+xml"/><item><title>Linear Regression from scratch</title><link>https://davethehuman.com/notes/linear-regression-from-scratch/</link><pubDate>Tue, 06 Oct 2026 00:00:00 +0000</pubDate><guid>https://davethehuman.com/notes/linear-regression-from-scratch/</guid><description>&lt;h2 class="relative group"&gt;Algorithm and implementation
 &lt;div id="algorithm-and-implementation" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#algorithm-and-implementation" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://davethehuman.com/notes/linear-regression/"&gt;Linear Regression&lt;/a&gt; formula can be replicated on numpy with &lt;code&gt;X @ w + b&lt;/code&gt; (or &lt;code&gt;np.dot(X,w)&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;To implement &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;gradient descent&lt;/a&gt; as an &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;optimizer&lt;/a&gt;, we need to calculate the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;errors = predictions - y&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The loss is &lt;a href="https://davethehuman.com/notes/rss/"&gt;RSS&lt;/a&gt; , evaluated with &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;MSE&lt;/a&gt;.&lt;br&gt;
&lt;code&gt;loss = np.mean(errors - y)**2&lt;/code&gt; In this case, we will actually be using the half-&lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;MSE&lt;/a&gt; to have similarities with &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;Logistic Regression from scratch&lt;/a&gt;.
$$L=\frac{1}{2N}\sum_i(\hat y_i-y_i)^2.$$Half-MSE has the same minimizer as MSE and its &lt;code&gt;1/2&lt;/code&gt; cancels the &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;derivative&lt;/a&gt;&amp;rsquo;s &lt;code&gt;2&lt;/code&gt; giving the same gradient template as &lt;a href="https://davethehuman.com/notes/logistic-regression/"&gt;Logistic Regression&lt;/a&gt;:
$$dw=X^T errors/N, \qquad db=mean(errors).$$
Using &amp;ldquo;pure&amp;rdquo; MSE the derivative would have been:
$$dw=2X^T errors/N, \qquad db=mean(errors).$$&lt;/li&gt;
&lt;li&gt;weights &lt;code&gt;w&lt;/code&gt; and &lt;code&gt;biases&lt;/code&gt; are initialized as &lt;code&gt;np.zeros&lt;/code&gt; and &lt;code&gt;np.mean(y)&lt;/code&gt; respectively, since the &lt;a href="https://davethehuman.com/notes/weights/"&gt;weights&lt;/a&gt; act as a sort of &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;feature importance&lt;/a&gt; and the &lt;a href="https://davethehuman.com/notes/bias/"&gt;bias&lt;/a&gt; works as an &lt;a href="https://davethehuman.com/notes/intercept/"&gt;intercept&lt;/a&gt; .
After choosing a &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;learning rate&lt;/a&gt;, at further steps &lt;code&gt;w&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; will be updated as:
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;w-=dw * learning_rate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;b-=db * learning_rate&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 class="relative group"&gt;Class structure
 &lt;div id="class-structure" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#class-structure" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;main attributes&lt;/strong&gt;:
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;learning_rate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n_iterations&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://davethehuman.com/notes/weights/"&gt;weights&lt;/a&gt; &lt;code&gt;w_&lt;/code&gt; and &lt;a href="https://davethehuman.com/notes/bias/"&gt;bias&lt;/a&gt; &lt;code&gt;b_&lt;/code&gt; initialized as &lt;code&gt;None&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fit&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;&lt;code&gt;predict&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; numpy &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; np
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;LinearRegression&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;__init__&lt;/span&gt;(self, learning_rate&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0.05&lt;/span&gt;, n_iterations&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1000&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;learning_rate &lt;span style="color:#f92672"&gt;=&lt;/span&gt; learning_rate
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;n_iterations &lt;span style="color:#f92672"&gt;=&lt;/span&gt; n_iterations
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;w_ &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;None&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;b_ &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;None&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fit&lt;/span&gt;(self, X, y):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#66d9ef"&gt;pass&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;predict&lt;/span&gt;(self, X):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#66d9ef"&gt;pass&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 class="relative group"&gt;Training
 &lt;div id="training" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#training" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;fit&lt;/code&gt; method can be implemented following these steps:&lt;/p&gt;</description></item><item><title>RSS</title><link>https://davethehuman.com/notes/rss/</link><pubDate>Tue, 29 Sep 2026 00:00:00 +0000</pubDate><guid>https://davethehuman.com/notes/rss/</guid><description>&lt;p&gt;The &lt;em&gt;&lt;a href="https://davethehuman.com/notes/residual/"&gt;Residual&lt;/a&gt; Sum of Squares (RSS)&lt;/em&gt; is the &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;loss function&lt;/a&gt; used in &lt;a href="https://davethehuman.com/notes/linear-regression/"&gt;Linear Regression&lt;/a&gt; to find the &lt;a href="https://davethehuman.com/notes/weights/"&gt;weights&lt;/a&gt;.&lt;/p&gt;
$$
\operatorname{RSS} = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2,
\qquad
\hat{y}_i = \beta_0 + x_i^{\mathsf{T}}\beta
$$&lt;p&gt;It is also the core error &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;metric&lt;/a&gt; used to build and optimize &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;regression&lt;/a&gt; &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;decision trees&lt;/a&gt; by selecting the best split that results in the lowest total RSS for the resulting &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;child nodes&lt;/a&gt;:&lt;/p&gt;
$$RSS = \sum_{i \in R_1} (y_i - \hat{y}_{R_1})^2 + \sum_{i \in R_2} (y_i - \hat{y}_{R_2})^2$$</description></item><item><title>residual</title><link>https://davethehuman.com/notes/residual/</link><pubDate>Sat, 26 Sep 2026 00:00:00 +0000</pubDate><guid>https://davethehuman.com/notes/residual/</guid><description>&lt;p&gt;The &lt;em&gt;residual&lt;/em&gt; is the distance between \(\tilde{y}\) (what the model predicted) and the actual value \(y\).&lt;/p&gt;
&lt;p&gt;It is particularly important for the &lt;a href="https://davethehuman.com/notes/linear-regression/"&gt;Linear Regression&lt;/a&gt; model, that minimizes &lt;a href="https://davethehuman.com/notes/rss/"&gt;residual sum of squares (RSS)&lt;/a&gt; to estimate &lt;a href="https://davethehuman.com/notes/weights/"&gt;weights&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Linear Regression</title><link>https://davethehuman.com/notes/linear-regression/</link><pubDate>Thu, 24 Sep 2026 00:00:00 +0000</pubDate><guid>https://davethehuman.com/notes/linear-regression/</guid><description>&lt;h2 class="relative group"&gt;Formula
 &lt;div id="formula" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#formula" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;When using &lt;em&gt;Linear Regression&lt;/em&gt;, the goal is to estimate a function $f(x)$ such that each &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;feature&lt;/a&gt; has a &lt;strong&gt;linear relationship&lt;/strong&gt; to the target variable $y$.&lt;br&gt;
This is a &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;supervised learning&lt;/a&gt; model written as:
&lt;/p&gt;
$$
y = \beta_0 + X\beta
$$&lt;p&gt;with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$\beta_0$ = &lt;a href="https://davethehuman.com/notes/intercept/"&gt;intercept&lt;/a&gt; (or &lt;a href="https://davethehuman.com/notes/bias/"&gt;bias&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;$X$ = matrix of &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;predictor&lt;/a&gt; variables&lt;/li&gt;
&lt;li&gt;$\beta$ = vector of parameters that determine the &lt;a href="https://davethehuman.com/notes/weights/"&gt;weights&lt;/a&gt; for each variable. For a single predictor, the sign of $\beta$ gives the direction of the relationship:
&lt;ul&gt;
&lt;li&gt;$\beta &gt; 0$ is positive&lt;/li&gt;
&lt;li&gt;$\beta &lt; 0$ is negative&lt;/li&gt;
&lt;li&gt;$\beta = 0$ means no linear effect.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;figure&gt;&lt;img
 class="my-0 rounded-md"
 loading="lazy"
 decoding="async"
 fetchpriority="auto"
 alt="linear regression"
 width="2000"
 height="1300"
 src="https://davethehuman.com/attachments/linear_regression_hu_12905239a6b86b3e.png"
 srcset="https://davethehuman.com/attachments/linear_regression_hu_12905239a6b86b3e.png 800w, https://davethehuman.com/attachments/linear_regression_hu_1cb26931883e32fb.png 1280w"
 sizes="(min-width: 768px) 50vw, 65vw"
 data-zoom-src="https://davethehuman.com/attachments/linear_regression.png"&gt;&lt;/figure&gt;
&lt;/p&gt;</description></item><item><title>intercept</title><link>https://davethehuman.com/notes/intercept/</link><pubDate>Sun, 20 Sep 2026 00:00:00 +0000</pubDate><guid>https://davethehuman.com/notes/intercept/</guid><description>&lt;p&gt;In &lt;a href="https://davethehuman.com/notes/linear-regression/"&gt;Linear Regression&lt;/a&gt;, the &lt;em&gt;intercept&lt;/em&gt; is the expected value of the &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;dependent variable&lt;/a&gt; $Y$ when all &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;independent variable&lt;/a&gt;s are equal to zero. Graphically, it is represented by the intersection point between the regression line and the Y axis on a chart; its value shift the regression line up and down without affecting its tilt or slope.&lt;/p&gt;
&lt;p&gt;&lt;figure&gt;&lt;img
 class="my-0 rounded-md"
 loading="lazy"
 decoding="async"
 fetchpriority="auto"
 alt="intercept"
 width="2000"
 height="1300"
 src="https://davethehuman.com/attachments/intercept_hu_426627f0b6687085.png"
 srcset="https://davethehuman.com/attachments/intercept_hu_426627f0b6687085.png 800w, https://davethehuman.com/attachments/intercept_hu_bf603842d8e23f87.png 1280w"
 sizes="(min-width: 768px) 50vw, 65vw"
 data-zoom-src="https://davethehuman.com/attachments/intercept.png"&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p&gt;It can be interpreted as a &lt;strong&gt;baseline value&lt;/strong&gt;, since it acts as the starting point or baseline prediction for the linear regression model before any independent variables effect is added.&lt;/p&gt;</description></item></channel></rss>