<?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[Decision Tree]]></title><description><![CDATA[Decision Tree]]></description><link>https://decision-tree.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 01:12:39 GMT</lastBuildDate><atom:link href="https://decision-tree.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🌳 Cracking Decision Trees: A Visual Guide to Classification in Python]]></title><description><![CDATA[“A Decision Tree is not just a model — it’s a flowchart of logic and learning.”
— Tilak Savani



🧠 Introduction
Decision Trees are one of the most intuitive and explainable machine learning algorithms. They mimic human decision-making and are used ...]]></description><link>https://decision-tree.hashnode.dev/cracking-decision-trees-a-visual-guide-to-classification-in-python</link><guid isPermaLink="true">https://decision-tree.hashnode.dev/cracking-decision-trees-a-visual-guide-to-classification-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[Decision Tree]]></category><dc:creator><![CDATA[Tilak Savani]]></dc:creator><pubDate>Mon, 07 Jul 2025 04:38:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1752557275496/f5c69f35-1e48-401c-9254-254306e5b54a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p>“A Decision Tree is not just a model — it’s a flowchart of logic and learning.”</p>
<p>— Tilak Savani</p>
</blockquote>
<hr />
<hr />
<h2 id="heading-introduction">🧠 Introduction</h2>
<p>Decision Trees are one of the most <strong>intuitive and explainable machine learning algorithms</strong>. They mimic human decision-making and are used for both <strong>classification</strong> and <strong>regression</strong> tasks.</p>
<p>In this blog, we’ll break down how they work — from logic to math — and implement one using <code>scikit-learn</code>.</p>
<hr />
<h2 id="heading-what-is-a-decision-tree">🤔 What Is a Decision Tree?</h2>
<p>A <strong>Decision Tree</strong> splits your dataset into smaller subsets based on feature values, forming a <strong>tree-like structure</strong>.<br />Each internal node asks a <strong>question</strong>, and the branches represent the <strong>answers</strong>.</p>
<p>Example:</p>
<pre><code class="lang-plaintext">          [Age &gt; 25?]
           /     \
         Yes     No
        /         \
   [Income &gt; 50K?]  Reject
</code></pre>
<hr />
<h2 id="heading-how-it-works-step-by-step">⚙️ How It Works (Step-by-Step)</h2>
<ol>
<li><p><strong>Start with all the data</strong>.</p>
</li>
<li><p>For each feature, calculate how "pure" the split would be.</p>
</li>
<li><p>Choose the best feature to split on (highest information gain or lowest Gini).</p>
</li>
<li><p>Repeat the process on each subset recursively until stopping criteria is met.</p>
</li>
</ol>
<hr />
<h2 id="heading-math-behind-decision-trees">🧮 Math Behind Decision Trees</h2>
<p>Decision Trees use criteria like <strong>Gini Impurity</strong> or <strong>Information Gain (Entropy)</strong> to decide the best split.</p>
<h3 id="heading-1-gini-impurity">✳️ 1. Gini Impurity</h3>
<p>Used in <code>sklearn</code> by default. Measures how "mixed" the labels are in a node.</p>
<pre><code class="lang-markdown"><span class="hljs-code">    Gini = 1 − Σ(pᵢ²)</span>
</code></pre>
<p>Where:</p>
<ul>
<li><code>pᵢ</code> = probability of class <code>i</code> in the node</li>
</ul>
<p>A <strong>pure node</strong> (all same class) has Gini = 0.</p>
<h3 id="heading-2-entropy-amp-information-gain">🔍 2. Entropy &amp; Information Gain</h3>
<p><strong>Entropy</strong> quantifies disorder:</p>
<pre><code class="lang-markdown"><span class="hljs-code">    Entropy = − Σ(pᵢ * log₂(pᵢ))</span>
</code></pre>
<p><strong>Information Gain</strong> is the <strong>reduction in entropy</strong> after a split:</p>
<pre><code class="lang-markdown"><span class="hljs-code">    Gain = Entropy(parent) − [Weighted avg. Entropy(children)]</span>
</code></pre>
<p>Decision Trees using entropy try to <strong>maximize gain</strong> at each node.</p>
<hr />
<h2 id="heading-python-code-classification-example">🧪 Python Code: Classification Example</h2>
<p>Let’s build a tree to classify whether a person buys a product based on age and salary.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd
<span class="hljs-keyword">from</span> sklearn.tree <span class="hljs-keyword">import</span> DecisionTreeClassifier
<span class="hljs-keyword">from</span> sklearn <span class="hljs-keyword">import</span> tree
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt

<span class="hljs-comment"># Sample Data</span>
data = {
    <span class="hljs-string">'Age'</span>: [<span class="hljs-number">22</span>, <span class="hljs-number">25</span>, <span class="hljs-number">47</span>, <span class="hljs-number">52</span>, <span class="hljs-number">46</span>, <span class="hljs-number">56</span>],
    <span class="hljs-string">'Salary'</span>: [<span class="hljs-number">15000</span>, <span class="hljs-number">29000</span>, <span class="hljs-number">48000</span>, <span class="hljs-number">60000</span>, <span class="hljs-number">52000</span>, <span class="hljs-number">61000</span>],
    <span class="hljs-string">'Buys'</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-number">1</span>]
}

df = pd.DataFrame(data)

X = df[[<span class="hljs-string">'Age'</span>, <span class="hljs-string">'Salary'</span>]]
y = df[<span class="hljs-string">'Buys'</span>]

<span class="hljs-comment"># Train Model</span>
clf = DecisionTreeClassifier(criterion=<span class="hljs-string">'entropy'</span>, max_depth=<span class="hljs-number">3</span>)
clf.fit(X, y)
</code></pre>
<hr />
<h2 id="heading-visualize-the-tree">📊 Visualize the Tree</h2>
<pre><code class="lang-python">plt.figure(figsize=(<span class="hljs-number">10</span>, <span class="hljs-number">6</span>))
tree.plot_tree(clf, filled=<span class="hljs-literal">True</span>, feature_names=[<span class="hljs-string">'Age'</span>, <span class="hljs-string">'Salary'</span>], class_names=[<span class="hljs-string">'No'</span>, <span class="hljs-string">'Yes'</span>])
plt.title(<span class="hljs-string">"Decision Tree for Product Purchase"</span>)
plt.show()
</code></pre>
<hr />
<h2 id="heading-predict">🧪 Predict</h2>
<pre><code class="lang-python"><span class="hljs-comment"># Predict for someone aged 30 with 40K salary</span>
print(clf.predict([[<span class="hljs-number">30</span>, <span class="hljs-number">40000</span>]]))  <span class="hljs-comment"># Output: [0] (Not likely to buy)</span>
</code></pre>
<hr />
<h2 id="heading-real-world-applications">🌍 Real-World Applications</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Industry</td><td>Use Case</td></tr>
</thead>
<tbody>
<tr>
<td>Finance</td><td>Loan approval (yes/no)</td></tr>
<tr>
<td>Health</td><td>Disease diagnosis (benign/malignant)</td></tr>
<tr>
<td>Retail</td><td>Predict customer churn</td></tr>
<tr>
<td>HR</td><td>Employee attrition prediction</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-advantages">✅ Advantages</h2>
<ul>
<li><p>Easy to understand and interpret</p>
</li>
<li><p>No need for feature scaling</p>
</li>
<li><p>Works with both numerical and categorical data</p>
</li>
</ul>
<hr />
<h2 id="heading-limitations">⚠️ Limitations</h2>
<ul>
<li><p>Prone to overfitting (can be solved by pruning or using ensembles like Random Forest)</p>
</li>
<li><p>Not as accurate as other models for complex datasets</p>
</li>
</ul>
<hr />
<h2 id="heading-final-thoughts">🧩 Final Thoughts</h2>
<p>Decision Trees are a powerful blend of logic, math, and machine learning. They're <strong>transparent</strong>, <strong>fast</strong>, and form the building block of <strong>ensemble models</strong> like Random Forest and Gradient Boosting.</p>
<p>Whether you're a beginner or building AI at scale, decision trees are a must-know algorithm.</p>
<hr />
<h2 id="heading-subscribe">📬 Subscribe</h2>
<p>If you liked this blog, follow me on Hasenode for more posts on Machine Learning and Python.</p>
<p>Thanks for reading! 😊</p>
]]></content:encoded></item></channel></rss>