๐ŸŒณ Decision Trees

Machine Learning for Beginners

Learn how computers make decisions just like humans.

๐ŸŒฒ What is a Tree?

In nature, a tree starts from a root, grows into branches, and ends with leaves. A Decision Tree follows the same concept.

Real Tree โžœ Root โžœ Branches โžœ Leaves
Decision Tree โžœ Question โžœ Answers โžœ Prediction

What is a Decision Tree?

A Decision Tree is a Supervised Machine Learning Algorithm used for Classification and Regression tasks. It learns patterns by asking questions and making decisions.

Decision Tree = Questions + Answers + Decisions

Types of Decision Trees

Classification Tree

  • Pass / Fail
  • Spam / Not Spam
  • Disease / No Disease
  • Buy / Don't Buy

Regression Tree

  • Salary Prediction
  • House Price Prediction
  • Stock Price Prediction
  • Temperature Prediction

๐Ÿ• Indian Pizza Dataset

Goal: Predict whether a student will order pizza.

Weather Pocket Money Friend Available Order Pizza
Hot 500 Yes Yes
Hot 400 Yes Yes
Cold 100 No No
Rainy 200 No No
Rainy 500 Yes Yes
Hot 600 No Yes

Input Features & Target

๐ŸŒค Weather
Input Feature
๐Ÿ’ฐ Pocket Money
Input Feature
๐Ÿ‘ฌ Friend Available
Input Feature
๐Ÿ• Order Pizza
Target

๐ŸŒณ How a Decision Tree Thinks

Dataset
โ†“
Weather ?
โ˜€๏ธ HOT
๐Ÿ• Order Pizza
โ„๏ธ COLD
โŒ Don't Order
๐ŸŒง๏ธ RAINY
๐Ÿ’ฐ Money > 300 ?
YES
๐Ÿ•
NO
โŒ

๐Ÿš€ How Decision Trees Learn

Step 1: Load Dataset
Step 2: Identify Features and Target
Step 3: Study Every Feature
Step 4: Choose Best Feature
Step 5: Create Branches
Step 6: Ask More Questions if Needed
Step 7: Make Final Prediction

๐Ÿ“ Summary

  • Decision Trees mimic human decision making.
  • Classification Trees predict categories.
  • Regression Trees predict numbers.
  • Features are inputs.
  • Target is the output.
  • Decision Trees ask questions before predicting.
PART 2

๐ŸŒณ Decision Tree Terminology & Structure

Understanding Root Nodes, Internal Nodes, Branches, Leaf Nodes, Pure Nodes and Impure Nodes

๐Ÿค” Why Does a Decision Tree Ask Questions?

Imagine you want to decide whether to play cricket. You don't randomly decide. You first ask questions.

๐ŸŒง Is it raining?
โœ” NO โ†’ Play Cricket
โŒ YES โ†’ Stay Home

Decision Trees work exactly the same way. They continuously ask questions until a final decision is reached.

๐Ÿ“š Decision Tree Terminology

๐ŸŒฑ

Root Node

Starting point of the tree

๐ŸŒฟ

Internal Node

Decision-making question

๐Ÿ”€

Branch

Path between nodes

๐Ÿ

Leaf Node

Final Prediction

๐ŸŒณ Visual Tree Structure

This is a real Decision Tree generated by a Machine Learning algorithm.

๐ŸŒฑ Root Node

The Root Node is the first question asked by the Decision Tree. It is the top-most node of the tree. Every Decision Tree contains only one Root Node.

Example:

Weather = Hot ?

๐ŸŒฟ Internal Node

Internal Nodes are additional questions asked after the Root Node. They help separate mixed data.

Example:

Pocket Money > โ‚น300 ?

๐Ÿ”€ Branch

Branches connect nodes together. They represent answers to questions.

Question Branches
Weather Hot? Yes / No
Money > 300? Yes / No

๐Ÿ Leaf Node

Leaf Nodes contain final predictions. No further questions are asked.

๐Ÿ• Order Pizza
โŒ Don't Order

โœ… Pure Node

A Pure Node contains only one class.

YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
Impurity = 0
Perfect Node

โš ๏ธ Impure Node

Impure Nodes contain multiple classes.

YES
YES
NO
YES
NO
Mixed Data
Decision Tree will try to split this node.

โš–๏ธ Pure Node vs Impure Node

Pure Node Impure Node
Only One Class Multiple Classes
Easy Prediction Difficult Prediction
Impurity = 0 Impurity > 0
Preferred Not Preferred

๐Ÿ Cricket Match Dataset

Goal: Predict whether a cricket match will be played.

Weather Ground Wet Play Match
Sunny No Yes
Sunny No Yes
Rainy Yes No
Rainy Yes No
Cloudy No Yes

๐Ÿš€ How a Tree is Built

Step 1
Load Data
Step 2
Ask Question
Step 3
Split Data
Step 4
Check Purity
Step 5
Split Again
Step 6
Leaf Node

๐Ÿ“ Part 2 Summary

  • Root Node is the first question asked.
  • Internal Nodes are additional questions.
  • Branches connect nodes.
  • Leaf Nodes contain final predictions.
  • Pure Nodes contain only one class.
  • Impure Nodes contain mixed classes.
  • Decision Trees split data to reduce impurity.
๐Ÿ‘‰ Next: Part 3 โ€“ Gini Impurity, Entropy and Information Gain
PART 3A

๐Ÿงฎ Why Splitting Happens?

Understanding how a Decision Tree chooses the first question before learning Gini Impurity.

๐ŸŽฏ Our Goal

We want to predict:

๐Ÿ• Will a Student Order Pizza?

To answer this question, we have collected data.

๐Ÿ“Š Pizza Ordering Dataset

Weather Pocket Money Friend Available Order Pizza
Hot 500 Yes Yes
Hot 400 Yes Yes
Cold 100 No No
Rainy 200 No No
Rainy 500 Yes Yes
Hot 600 No Yes

๐Ÿ˜ต Step 1 : All Data is Mixed Together

Initially, the Decision Tree has not asked any questions. All records are inside a single node.

YES
YES
NO
NO
YES
YES

Some students ordered pizza. Some students did not order pizza.

The tree is confused.

๐ŸŒณ Initial Tree

Currently all records are inside one node.

โš–๏ธ Pure Node vs Impure Node

Pure Node

YES
YES
YES
YES

No confusion. All records belong to one class.

Impure Node

YES
YES
NO
YES
NO

Mixed classes. The tree is confused.

โœ‚๏ธ Step 2 : Why Does Splitting Happen?

Decision Trees hate confusion.

The goal of the tree is:

Convert Impure Nodes โžก Into Pure Nodes

๐Ÿง Step 3 : Which Question Should Be Asked First?

The tree can choose any feature.

๐ŸŒค Weather ?
๐Ÿ’ฐ Pocket Money ?
๐Ÿ‘ฌ Friend Available ?

But which one is best?

The tree tests each feature one by one.

๐ŸŒค Step 4 : Testing Weather

The tree creates a temporary split.

Weather Pizza Result
Hot YES
YES
YES
Cold NO
Rainy YES
NO

๐Ÿ” Step 5 : Analyze the Split

HOT

YES
YES
YES

Pure Node โœ…

COLD

NO

Pure Node โœ…

RAINY

YES
NO

Impure Node โŒ

๐Ÿคฏ How Good Is This Split?

Weather created:

  • 2 Pure Nodes
  • 1 Impure Node

This seems good. But can another feature do even better?

The tree now tests Pocket Money. Then Friend Available.

๐Ÿ“ We Need a Score

The tree needs a numerical score to compare features.

Feature A โ†’ Good Split
Feature B โ†’ Better Split
Feature C โ†’ Best Split

How do we measure this?

Gini Impurity

In Part 3B, we will learn exactly how Gini is calculated.

๐Ÿ“ Part 3A Summary

  • All records start inside one node.
  • Mixed data creates confusion.
  • Mixed nodes are called Impure Nodes.
  • Decision Trees try to create Pure Nodes.
  • The tree tests every feature.
  • Each feature creates a temporary split.
  • The tree evaluates every split.
  • To compare splits, the tree uses Gini Impurity.
๐Ÿ‘‰ Next: Part 3B โ€“ Gini Impurity Formula, Step-by-Step Calculations & Weighted Gini
PART 3B

๐Ÿงฎ Gini Impurity & Choosing the Best Split

How does a Decision Tree compare features and choose the Root Node?

๐ŸŽฏ Step 1 : We Need a Score

In Part 3A, we tested Weather. The split looked good.

But how can we prove it is good?

Decision Trees need a score.

This score is called:

Gini Impurity

๐Ÿค” What Does Gini Measure?

Gini measures confusion.

YES
YES
YES
YES

No confusion. Pure Node.

YES
YES
NO
NO

Confusing. Impure Node.

More confusion โ†’ Higher Gini
Less confusion โ†’ Lower Gini

๐Ÿ“ Gini Formula

Gini = 1 โˆ’ P(Yes)ยฒ โˆ’ P(No)ยฒ


P(Yes) = Probability of YES

P(No) = Probability of NO

๐Ÿงช Example 1 : Pure Node

YES
YES
YES
YES

Total Records = 4

P(Yes)=4/4=1

P(No)=0/4=0



Gini

= 1 - (1)ยฒ - (0)ยฒ

= 1 - 1 - 0

= 0

Gini = 0
Perfect Node

๐Ÿงช Example 2 : Impure Node

YES
NO

P(Yes)=1/2=0.5

P(No)=1/2=0.5



Gini

= 1 - (0.5)ยฒ - (0.5)ยฒ

= 1 - 0.25 - 0.25

= 0.5

Gini = 0.5
Very Mixed Node

๐ŸŒค Step 6 : Calculate Weather Split

HOT Node

YES
YES
YES

P(Yes)=1

P(No)=0

Gini

=1-(1ยฒ)-(0ยฒ)

=0

COLD Node

NO

P(Yes)=0

P(No)=1

Gini

=0

RAINY Node

YES
NO

P(Yes)=0.5

P(No)=0.5

Gini

=1-(0.5ยฒ)-(0.5ยฒ)

=0.5

โš–๏ธ Weighted Gini

Not every node contains the same number of records.

Node Records Gini
Hot 3 0
Cold 1 0
Rainy 2 0.5

๐Ÿงฎ Final Weather Gini


Weighted Gini

=

(3/6 ร— 0)

+

(1/6 ร— 0)

+

(2/6 ร— 0.5)

=

0.167

Weather Gini = 0.167

๐Ÿ† Comparing Features

Feature Weighted Gini
Weather 0.167
Pocket Money 0.25
Friend Available 0.33
Lowest Gini Wins

๐ŸŒณ Root Node Selection

Weather becomes the Root Node because it has the Lowest Gini.

๐Ÿ“ Part 3B Summary

  • Gini measures confusion.
  • Pure nodes have Gini = 0.
  • Mixed nodes have higher Gini.
  • Each split creates multiple nodes.
  • Each node gets its own Gini.
  • Weighted Gini combines all node scores.
  • The feature with the lowest Weighted Gini wins.
  • The winning feature becomes the Root Node.
๐Ÿ‘‰ Next Part 4: Entropy & Information Gain