<?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"><channel><title><![CDATA[Linear Regression in Python]]></title><description><![CDATA[Linear Regression in Python]]></description><link>https://linear-regression-in-python.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 15:44:39 GMT</lastBuildDate><atom:link href="https://linear-regression-in-python.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🎯 "Predicting the Future: A Hands-On Guide to Linear Regression Using Python"]]></title><description><![CDATA["Machine learning is not magic — it’s math made powerful."
— Tilak Savani



🧠 Introduction
Machine learning is transforming industries, from finance to healthcare. At the heart of it lies a simple but powerful tool — Linear Regression. Whether you'...]]></description><link>https://linear-regression-in-python.hashnode.dev/predicting-the-future-a-hands-on-guide-to-linear-regression-using-python</link><guid isPermaLink="true">https://linear-regression-in-python.hashnode.dev/predicting-the-future-a-hands-on-guide-to-linear-regression-using-python</guid><category><![CDATA[Machine Learning]]></category><category><![CDATA[linearregression]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Tilak Savani]]></dc:creator><pubDate>Fri, 04 Jul 2025 06:46:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1751697268330/f2a84ad7-48d2-403a-88c3-7c3e06b80a9f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p>"Machine learning is not magic — it’s math made powerful."</p>
<p>— Tilak Savani</p>
</blockquote>
<hr />
<hr />
<h2 id="heading-introduction">🧠 Introduction</h2>
<p>Machine learning is transforming industries, from finance to healthcare. At the heart of it lies a simple but powerful tool — <strong>Linear Regression</strong>. Whether you're predicting housing prices or analyzing stock trends, linear regression is often your first step into predictive modeling.</p>
<p>In this post, we'll break it down step-by-step and implement it using Python’s most popular ML library — <strong>scikit-learn</strong>.</p>
<hr />
<h2 id="heading-what-is-linear-regression">📈 What is Linear Regression?</h2>
<p>Linear Regression is a <strong>supervised learning algorithm</strong> used for <strong>predicting continuous values</strong>. It finds the best-fitting straight line (called the regression line) through your data points.</p>
<h3 id="heading-the-formula">The formula:</h3>
<pre><code class="lang-markdown"><span class="hljs-code">    y = mx + c</span>
</code></pre>
<h3 id="heading-where">Where:</h3>
<ul>
<li><p>y: predicted output</p>
</li>
<li><p>x: input features</p>
</li>
<li><p>m: slope (weights)</p>
</li>
<li><p>c: intercept (bias)</p>
</li>
</ul>
<hr />
<h2 id="heading-how-it-works-step-by-step">⚙️ How It Works (Step-by-Step)</h2>
<h3 id="heading-1-collecting-the-data">1. Collecting the Data</h3>
<p>We'll use a simple dataset: <strong>Hours Studied vs Exam Score</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Hours Studied</td><td>Exam Score</td></tr>
</thead>
<tbody>
<tr>
<td>1.5</td><td>40</td></tr>
<tr>
<td>3.0</td><td>60</td></tr>
<tr>
<td>4.5</td><td>75</td></tr>
<tr>
<td>5.5</td><td>82</td></tr>
<tr>
<td>6.0</td><td>90</td></tr>
</tbody>
</table>
</div><h3 id="heading-2-visualizing-the-data">2. Visualizing the Data</h3>
<p>Plotting helps us understand the trend — does more studying lead to better scores?</p>
<h3 id="heading-3-building-the-model">3. Building the Model</h3>
<p>We'll use <strong>scikit-learn</strong> to define the linear regression model.</p>
<h3 id="heading-4-training-the-model">4. Training the Model</h3>
<p>Feed data to the model so it learns the relationship between study time and scores.</p>
<h3 id="heading-5-making-predictions">5. Making Predictions</h3>
<p>Once trained, we can predict scores based on study hours.</p>
<hr />
<h2 id="heading-step-by-step-mathematical-derivation">🧮 Step-by-Step Mathematical Derivation</h2>
<p>We’ll now calculate the best-fit line manually using the <strong>least squares method</strong>.</p>
<h3 id="heading-step-1-calculate-the-means">Step 1: Calculate the means</h3>
<pre><code class="lang-markdown"><span class="hljs-code">    x̄ = mean of x values  
    ȳ = mean of y values</span>
</code></pre>
<p>From our data:</p>
<pre><code class="lang-markdown"><span class="hljs-code">    x̄ = (1.5 + 3 + 4.5 + 5.5 + 6) / 5 = 4.1  
    ȳ = (40 + 60 + 75 + 82 + 90) / 5 = 69.4</span>
</code></pre>
<h3 id="heading-step-2-calculate-slope-m">Step 2: Calculate slope (m)</h3>
<pre><code class="lang-markdown"><span class="hljs-code">    m = Σ[(xᵢ -x̄)(yᵢ - ȳ)] / Σ[(xᵢ - x̄)²]</span>
</code></pre>
<p>After calculation:</p>
<pre><code class="lang-nginx"><span class="hljs-attribute">m</span> ≈ <span class="hljs-number">10</span>.<span class="hljs-number">64</span>
</code></pre>
<h3 id="heading-step-3-calculate-intercept-c">Step 3: Calculate intercept (c)</h3>
<pre><code class="lang-markdown"><span class="hljs-code">    c = ȳ - m * x̄</span>
</code></pre>
<pre><code class="lang-nginx"><span class="hljs-attribute">c</span> = <span class="hljs-number">69</span>.<span class="hljs-number">4</span> - <span class="hljs-number">10</span>.<span class="hljs-number">64</span> * <span class="hljs-number">4</span>.<span class="hljs-number">1</span> ≈ <span class="hljs-number">25</span>.<span class="hljs-number">78</span>
</code></pre>
<h3 id="heading-final-linear-regression-equation">✅ Final Linear Regression Equation</h3>
<pre><code class="lang-plaintext">y = 10.64x + 25.78
</code></pre>
<p>So, every extra hour of study increases predicted score by ~10.64 points, and even with zero study hours, a student is expected to score ~25.78 marks.</p>
<hr />
<h2 id="heading-code-implementation">🧪 Code Implementation</h2>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">from</span> sklearn.linear_model <span class="hljs-keyword">import</span> LinearRegression

<span class="hljs-comment"># Data</span>
X = np.array([<span class="hljs-number">1.5</span>, <span class="hljs-number">3.0</span>, <span class="hljs-number">4.5</span>, <span class="hljs-number">5.5</span>, <span class="hljs-number">6.0</span>]).reshape(<span class="hljs-number">-1</span>, <span class="hljs-number">1</span>)
y = np.array([<span class="hljs-number">40</span>, <span class="hljs-number">60</span>, <span class="hljs-number">75</span>, <span class="hljs-number">82</span>, <span class="hljs-number">90</span>])

<span class="hljs-comment"># Train the model</span>
model = LinearRegression()
model.fit(X, y)

<span class="hljs-comment"># Predict</span>
predicted = model.predict(X)

<span class="hljs-comment"># Plot</span>
plt.scatter(X, y, color=<span class="hljs-string">'blue'</span>, label=<span class="hljs-string">'Actual'</span>)
plt.plot(X, predicted, color=<span class="hljs-string">'red'</span>, label=<span class="hljs-string">'Regression Line'</span>)
plt.xlabel(<span class="hljs-string">"Hours Studied"</span>)
plt.ylabel(<span class="hljs-string">"Exam Score"</span>)
plt.title(<span class="hljs-string">"Linear Regression Example"</span>)
plt.legend()
plt.show()

<span class="hljs-comment"># Show equation</span>
print(<span class="hljs-string">f"Equation: y = <span class="hljs-subst">{model.coef_[<span class="hljs-number">0</span>]:<span class="hljs-number">.2</span>f}</span>x + <span class="hljs-subst">{model.intercept_:<span class="hljs-number">.2</span>f}</span>"</span>)
</code></pre>
<h3 id="heading-sample-output">📊 Sample Output</h3>
<pre><code class="lang-yaml"><span class="hljs-attr">Equation:</span> <span class="hljs-string">y</span> <span class="hljs-string">=</span> <span class="hljs-number">10.</span><span class="hljs-string">61x</span> <span class="hljs-string">+</span> <span class="hljs-number">24.37</span>
</code></pre>
<p>This means every extra hour studied increases score by approx 10.61 points!</p>
<hr />
<h2 id="heading-why-linear-regression-matters">🧠 Why Linear Regression Matters</h2>
<ul>
<li><p>✅ Simple yet powerful</p>
</li>
<li><p>✅ Great baseline model</p>
</li>
<li><p>✅ Easy to interpret</p>
</li>
<li><p>✅ Works well when data has a linear trend</p>
</li>
</ul>
<hr />
<h2 id="heading-real-world-applications">🌐 Real-World Applications</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Domain</td><td>Use Case</td></tr>
</thead>
<tbody>
<tr>
<td>Finance</td><td>Predicting stock prices</td></tr>
<tr>
<td>Healthcare</td><td>Estimating disease progression</td></tr>
<tr>
<td>Business</td><td>Sales forecasting</td></tr>
<tr>
<td>Sports</td><td>Performance analytics</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-final-thoughts">🧩 Final Thoughts</h2>
<p>Linear Regression is your first step into the exciting world of <strong>Machine Learning</strong>. It’s intuitive, useful, and forms the base for many advanced models. Mastering it gives you a solid foundation to explore techniques like polynomial regression, decision trees, and even deep learning.</p>
<hr />
<h2 id="heading-subscribe-to-my-blog">✉️ Subscribe to my blog!</h2>
<p>Stay tuned for more hands-on ML tutorials and projects. 🚀 Follow me on Hasenode and let's grow together in the world of Artificial Intelligence.</p>
]]></content:encoded></item></channel></rss>