<?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[Logistic Regression in Python]]></title><description><![CDATA[Logistic Regression in Python]]></description><link>https://logistic-regression-in-python.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 15 Nov 2026 22:53:18 GMT</lastBuildDate><atom:link href="https://logistic-regression-in-python.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🔐 Classification Made Simple: A Beginner’s Guide to Logistic Regression in Python]]></title><description><![CDATA[“If linear regression predicts numbers, logistic regression predicts decisions.” — Tilak Savani Logistic Regression is one of the most fundamental…]]></description><link>https://logistic-regression-in-python.hashnode.dev/classification-made-simple-a-beginners-guide-to-logistic-regression-in-python</link><guid isPermaLink="true">https://logistic-regression-in-python.hashnode.dev/classification-made-simple-a-beginners-guide-to-logistic-regression-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[logistic regression]]></category><category><![CDATA[Machine Learning]]></category><dc:creator><![CDATA[Tilak Savani]]></dc:creator><pubDate>Sat, 05 Jul 2025 04:06:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1752557332055/34868bb1-5d1a-44e5-8142-1bd972111b58.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p>“If linear regression predicts numbers, logistic regression predicts decisions.”</p>
<p>— Tilak Savani</p>
</blockquote>
<hr />
<hr />
<h2 id="heading-introduction">🧠 Introduction</h2>
<p>Logistic Regression is one of the most fundamental techniques in machine learning for classification problems. While linear regression predicts continuous values, logistic regression helps predict categories — like yes/no, pass/fail, or spam/not spam.</p>
<p>If you're stepping into classification, this is a great place to begin.</p>
<hr />
<h2 id="heading-what-is-logistic-regression">❓ What is Logistic Regression?</h2>
<p>Logistic Regression is a supervised learning algorithm used to classify data into binary classes (e.g., 0 or 1).</p>
<p>Instead of fitting a straight line like linear regression, it fits an S-shaped curve — called the sigmoid — that maps predicted values between 0 and 1.</p>
<hr />
<h2 id="heading-how-it-works">⚙️ How It Works</h2>
<h3 id="heading-1-sigmoid-function">1. Sigmoid Function</h3>
<p>The logistic regression model uses the <strong>sigmoid function</strong>:</p>
<pre><code class="lang-markdown"><span class="hljs-code">    σ(z) = 1 / (1 + e^(-z))</span>
</code></pre>
<p>Where <code>z = mx + c</code> (similar to linear regression)</p>
<ul>
<li><p>If σ(z) &gt; 0.5, we predict class 1</p>
</li>
<li><p>If σ(z) &lt; 0.5, we predict class 0</p>
</li>
</ul>
<h3 id="heading-2-decision-boundary">2. Decision Boundary</h3>
<p>The decision boundary is the threshold (usually 0.5) that splits the two classes. You can adjust this threshold to make the model more or less strict depending on your use case.</p>
<hr />
<h2 id="heading-use-case-will-a-student-pass">📦 Use Case: Will a Student Pass?</h2>
<p>Let’s predict whether a student will pass an exam based on how many hours they studied.</p>
<p>Label:</p>
<ul>
<li><p>1 = Pass</p>
</li>
<li><p>0 = Fail</p>
</li>
</ul>
<p>Example dataset:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Hours Studied</td><td>Passed</td></tr>
</thead>
<tbody>
<tr>
<td>1.0</td><td>0</td></tr>
<tr>
<td>2.0</td><td>0</td></tr>
<tr>
<td>3.0</td><td>0</td></tr>
<tr>
<td>4.0</td><td>1</td></tr>
<tr>
<td>5.0</td><td>1</td></tr>
<tr>
<td>6.0</td><td>1</td></tr>
</tbody>
</table>
</div><h3 id="heading-code-implementation-in-python">🧪 Code Implementation in Python</h3>
<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> LogisticRegression

<span class="hljs-comment"># Training Data</span>
X = np.array([[<span class="hljs-number">1.0</span>], [<span class="hljs-number">2.0</span>], [<span class="hljs-number">3.0</span>], [<span class="hljs-number">4.0</span>], [<span class="hljs-number">5.0</span>], [<span class="hljs-number">6.0</span>]])
y = np.array([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>])

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

<span class="hljs-comment"># Prediction</span>
X_test = np.linspace(<span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">100</span>).reshape(<span class="hljs-number">-1</span>, <span class="hljs-number">1</span>)
y_prob = model.predict_proba(X_test)[:, <span class="hljs-number">1</span>]
y_pred = model.predict(X_test)

<span class="hljs-comment"># Plotting</span>
plt.plot(X_test, y_prob, label=<span class="hljs-string">"Probability of Passing"</span>)
plt.axhline(<span class="hljs-number">0.5</span>, color=<span class="hljs-string">'red'</span>, linestyle=<span class="hljs-string">'--'</span>, label=<span class="hljs-string">'Decision Boundary (0.5)'</span>)
plt.scatter(X, y, c=y, cmap=<span class="hljs-string">'bwr'</span>, label=<span class="hljs-string">'Training Data'</span>)
plt.xlabel(<span class="hljs-string">"Hours Studied"</span>)
plt.ylabel(<span class="hljs-string">"Probability / Outcome"</span>)
plt.title(<span class="hljs-string">"Logistic Regression: Pass Prediction"</span>)
plt.legend()
plt.show()

<span class="hljs-comment"># Predict for a new value</span>
new_hours = np.array([[<span class="hljs-number">3.5</span>]])
prediction = model.predict(new_hours)[<span class="hljs-number">0</span>]
print(<span class="hljs-string">f"Prediction for 3.5 hours studied: <span class="hljs-subst">{<span class="hljs-string">'Pass'</span> <span class="hljs-keyword">if</span> prediction == <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'Fail'</span>}</span>"</span>)
</code></pre>
<h3 id="heading-output-amp-interpretation">📊 Output &amp; Interpretation</h3>
<pre><code class="lang-yaml"><span class="hljs-attr">Prediction for 3.5 hours studied:</span> <span class="hljs-string">Pass</span>
</code></pre>
<p>As the number of hours increases, so does the probability of passing. At 3.5 hours, the model predicts the student is likely to pass — just over the 0.5 threshold.</p>
<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>Healthcare</td><td>Disease diagnosis (sick or healthy)</td></tr>
<tr>
<td>Finance</td><td>Loan approval (yes or no)</td></tr>
<tr>
<td>Marketing</td><td>Email open prediction (open or not)</td></tr>
<tr>
<td>HR</td><td>Employee retention (stay or leave)</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-final-thoughts">🧩 Final Thoughts</h2>
<p>Logistic Regression is a powerful yet simple classification algorithm that is perfect for beginners. It’s interpretable, fast, and great for binary outcomes. Once you master it, you’ll be ready to explore more advanced classifiers like Decision Trees, Random Forests, and Neural Networks.</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 Hash node and let's grow together in the world of Artificial Intelligence.</p>
]]></content:encoded></item></channel></rss>